sql脚本获取汉字首字母的2种方法:
1、
create function GetPY(@str varchar(500))
returns varchar(500)
as
begin
declare @cyc int,@length int,@str1 varchar(100),@charcate varbinary(20)
set @cyc=1--从第几个字开始取
set @length=len(@str)--输入汉字的长度
set @str1=''--用于存放返回值
while @cyc<=@length
begin
select @charcate=cast(substring(@str,@cyc,1) as varbinary)--每次取出一个字并将其转变成二进制,便于与GBK编码表进行比较
if @charcate>=0XB0A1 and @charcate<=0XB0C4
set @str1=@str1+'A'--说明此汉字的首字母为A,以下同上
else if @charcate>=0XB0C5 and @charcate<=0XB2C0
set @str1=@str1+'B'
else if @charcate>=0XB2C1 and @charcate<=0XB4ED
set @str1=@str1+'C'
else if @charcate>=0XB4EE and @charcate<=0XB6E9
set @str1=@str1+'D'
else if @charcate>=0XB6EA and @charcate<=0XB7A1
set @str1=@str1+'E'
else if @charcate>=0XB7A2 and @charcate<=0XB8C0
set @str1=@str1+'F'
else if @charcate>=0XB8C1 and @charcate<=0XB9FD