sql存储过程获取汉字拼音头字母函数

复制代码 代码如下:

–函数

create function fn_getpy(@str nvarchar(4000))

returns nvarchar(4000)

–with encryption

as

begin

declare @intlenint

declare @strretnvarchar(4000)

declare @temp nvarchar(100)

set @intlen = len(@str)

set @strret = ”

while @intlen > 0

begin

set @temp = ”

select @temp = case

when substring(@str,@intlen,1) >= ‘帀’ then ‘z’

when substring(@str,@intlen,1) >= ‘丫’ then ‘y’

when substring(@str,@intlen,1) >= ‘夕’ then ‘x’

when substring(@str,@intlen,1) >= ‘屲’ then ‘w’

when substring(@str,@intlen,1) >= ‘他’ then ‘t’

when substring(@str,@intlen,1) >= ‘仨’ then ‘s’

when substring(@str,@intlen,1) >= ‘呥’ then ‘r’

when substring(@str,@intlen,1) >= ‘七’ then ‘q’

when substring(@str,@intlen,1) >= ‘妑’ then ‘p’

when substring(@str,@intlen,1) >= ‘噢’ then ‘o’

when substring(@str,@intlen,1) >= ‘拏’ then ‘n’

when substring(@str,@intlen,1) >= ‘嘸’ then ‘m’

when substring(@str,@intlen,1) >= ‘垃’ then ‘l’

when substring(@str,@intlen,1) >= ‘咔’ then ‘k’

when substring(@str,@intlen,1) >= ‘丌’ then ‘j’

when substring(@str,@intlen,1) >= ‘铪’ then ‘h’

when substring(@str,@intlen,1) >= ‘旮’ then ‘g’

when substring(@str,@intlen,1) >= ‘发’ then ‘f’

when substring(@str,@intlen,1) >= ‘妸’ then ‘e’

when substring(@str,@intlen,1) >= ‘咑’ then ‘d’

when substring(@str,@intlen,1) >= ‘嚓’ then ‘c’

when substring(@str,@intlen,1) >= ‘八’ then ‘b’

when substring(@str,@intlen,1) >= ‘吖’ then ‘a’

else rtrim(ltrim(substring(@str,@intlen,1)))

end

–对于汉字特殊字符,不生成拼音码

if (ascii(@temp)>127) set @temp = ”

–对于英文中小括号,不生成拼音码

if @temp = ‘(‘ or @temp = ‘)’ set @temp = ”

select @strret = @temp + @strret

set @intlen = @intlen – 1

end

return lower(@strret)

end

go

–调用

select dbo.fn_getpy(‘张三’)

–返回:zs

答!: 2:

取汉字拼音首字母的存储过程

create function fun_getpy ( @str nvarchar(4000) )

returns nvarchar(4000)

as

begin

declare @word nchar(1),@py nvarchar(4000)

set @py=”

while len(@str)>0

begin

set @word=left(@str,1)

–如果非汉字字符,返回原字符

set @py=@py+(case when unicode(@word) between 19968 and 19968+20901

then (

select top 1 py

from

(

select ‘a’ as py,n’驁’ as word

union all select ‘b’,n’簿’

union all select ‘c’,n’錯’

union all select ‘d’,n’鵽’

union all select ‘e’,n’樲’

union all select ‘f’,n’鰒’

union all select ‘g’,n’腂’

union all select ‘h’,n’夻’

union all select ‘j’,n’攈’

union all select ‘k’,n’穒’

union all select ‘l’,n’鱳’

union all select ‘m’,n’旀’

union all select ‘n’,n’桛’

union all select ‘o’,n’漚’

union all select ‘p’,n’曝’

union all select ‘q’,n’囕’

union all select ‘r’,n’鶸’

union all select ‘s’,n’蜶’

union all select ‘t’,n’籜’

union all select ‘w’,n’鶩’

union all select ‘x’,n’鑂’

union all select ‘y’,n’韻’

union all select ‘z’,n’咗’

) t

where word>=@word collate chinese_prc_cs_as_ks_ws

order by py asc

)

else @word

end)

set @str=right(@str,len(@str)-1)

end

return @py

end

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐