日期:2014-05-17  浏览次数:20749 次

求一个字符串转换为asc的函数
ASCII(),只会取字符串的第一个字付的ASCII值,有没有办法取整个字符串的ASCII值的和(SQL 2005 )

字符串只包含数字和字母,不会有中文字,但长度不定。



------解决方案--------------------
declare @str varchar(50)='abcde'
declare @asc int=0
while LEN(@str)>0
begin
set @asc=@asc+ascii(LEFT(@str,1))
set @str=SUBSTRING(@str,2,LEN(@str))
end
select @asc
------解决方案--------------------
SQL code
create function F_getasc(@str varchar(max))
returns @tb table(A int)
as
begin
declare @asc int=0
while LEN(@str)>0
begin
set @asc=@asc+ascii(LEFT(@str,1))
set @str=SUBSTRING(@str,2,LEN(@str))
end
insert into @tb values(@asc)
return
end
--测试
select * from f_getasc('abc')