sql server自动生成拼音首字母的函数

建立一个查询,执行下面的语句生成函数fn_getpy


复制代码 代码如下:

–生成拼音首码

create function fn_getpy(@str nvarchar(4000))

returns nvarchar(4000)

–with encryption

as

begin

declare @intlen int

declare @strret nvarchar(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

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

测试:

select product_id , dbo.fn_getpy(product_name) as pymc

from dbo.t_product

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

相关推荐