读书人

字符串转换为asc的函数

发布时间: 2012-11-17 11:14:15 作者: rapoo

求一个字符串转换为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)asbegindeclare @asc int=0while LEN(@str)>0beginset @asc=@asc+ascii(LEFT(@str,1))set @str=SUBSTRING(@str,2,LEN(@str))endinsert into @tb values(@asc)returnend--测试select * from f_getasc('abc') 

读书人网 >SQL Server

热点推荐