为什么无法取到字符的ASCII码值?

lingyun410 2010-06-12 05:18:17

declare @keyword nVarChar(1000)
set @keyword='a#b'
declare @strReturn nVarChar(1000)
,@tempStr nVarChar(1000)
,@tempItem int
,@tempCount int
,@tempChar nvarchar(10)
,@tempAscNum int
set @strReturn=''
set @tempCount=len(@keyword)
set @tempItem=1
while @tempItem<=@tempCount
begin
set @tempChar=substring(@keyword,@tempItem,1)
print @tempChar
set @tempAscNum=AscII(@tempChar)
print '-'&AscII(@tempChar)
if (@tempAscNum>=48 And @tempAscNum<=57) Or (@tempAscNum>=65 And @tempAscNum<=90) Or (@tempAscNum>=97 And @tempAscNum<=122)
begin
set @tempStr=@tempStr+@tempChar
end
set @tempItem=@tempItem+1
end
print @tempStr

为什么AscII(@tempChar)打印出来的ASCII码值是0?而单独使用AscII('a')能获取值。
...全文
145 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingyun410 2010-06-13
  • 打赏
  • 举报
回复
Create Function dbo.F_Get_FilterWord(
@Key Varchar(1000)
,@Type VarChar(4)
,@IgnoreCase VarChar(4)
)
Returns VarChar(1000)
As
Begin
Declare @Pattern VarChar(100)
,@PattChar VarChar(20)
,@PattNum VarChar(20)
,@PattZH VarChar(20)
Set @PattChar=
Case Upper(@IgnoreCase)
When '' Then 'a-zA-Z'
When 'L' Then 'a-z'
When 'U' Then 'A-Z'
End
Set @PattNum='0-9'
Set @PattZH='吖-咗'
Set @Pattern=
Case Upper(@Type)
When '' Then @PattChar+@PattNum+@PattZH
When 'C' Then @PattChar
When 'N' Then @PattNum
When 'Z' Then @PattZH
End
While Patindex('%[^'+@Pattern+']%',@Key)>0
Begin
Set @Key=Stuff(@Key,Patindex('%[^'+@Pattern+']%',@Key),1,'')
End
Return @Key
End

函数写出来了,但是却无法过滤é这个字符,难道这个字符属于汉字的范围?'吖-咗'包括这个字符?
lingyun410 2010-06-13
  • 打赏
  • 举报
回复
想实现一个过滤功能的函数
用来取掉除汉字、字母和数字以外的字符。
自己刚尝试写就遇到了这么多问题!
-狙击手- 2010-06-12
  • 打赏
  • 举报
回复
汗:


print '-'&AscII(@s)

你想干什么呀
永生天地 2010-06-12
  • 打赏
  • 举报
回复
declare @keyword nVarChar(1000)
set @keyword='a#b'
declare @strReturn nVarChar(1000)
,@tempStr nVarChar(1000)
,@tempItem int
,@tempCount int
,@tempChar nvarchar(10)
,@tempAscNum int
set @strReturn=''
set @tempCount=len(@keyword)
set @tempItem=1
while @tempItem<=@tempCount
begin
set @tempChar=substring(@keyword,@tempItem,1)
print @tempChar
set @tempAscNum=AscII(@tempChar)
print '-'&AscII(@tempChar)
if (@tempAscNum>=48 And @tempAscNum<=57) Or (@tempAscNum>=65 And @tempAscNum<=90) Or (@tempAscNum>=97 And @tempAscNum<=122)
begin
set @tempStr=@tempStr+@tempChar
end
set @tempItem=@tempItem+1
end
print @tempStr
/*
a
0
#
0
b
0
*/

改一改
declare @keyword nVarChar(1000)
set @keyword='a#b'
declare @strReturn nVarChar(1000)
,@tempStr nVarChar(1000)
,@tempItem int
,@tempCount int
,@tempChar nvarchar(10)
,@tempAscNum int
set @strReturn=''
set @tempCount=len(@keyword)
set @tempItem=1
while @tempItem<=@tempCount
begin
set @tempChar=substring(@keyword,@tempItem,1)
print @tempChar
set @tempAscNum=AscII(@tempChar)
print '-'+AscII(@tempChar)
if (@tempAscNum>=48 And @tempAscNum<=57) Or (@tempAscNum>=65 And @tempAscNum<=90) Or (@tempAscNum>=97 And @tempAscNum<=122)
begin
set @tempStr=@tempStr+@tempChar
end
set @tempItem=@tempItem+1
end
print @tempStr

/*
a
97
#
35
b
98

*/
lingyun410 2010-06-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xys_777 的回复:]

print '-'&AscII(@tempChar)
这个什么意图
[/Quote]
打印看一下,结果输出的是0
永生天地 2010-06-12
  • 打赏
  • 举报
回复
print '-'&AscII(@tempChar)
这个什么意图
lingyun410 2010-06-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]

汉字是两个字节,ascII只能取到第一个字节的asc码

用unicode取unicode码
[/Quote]
不是很明白我上面取的是字母
永生天地 2010-06-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]
汉字是两个字节,ascII只能取到第一个字节的asc码

用unicode取unicode码
[/Quote]
nvarchar是两个字节
永生天地 2010-06-12
  • 打赏
  • 举报
回复
汉字是两个字节,ascII只能取到第一个字节的asc码

用unicode取unicode码

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧