SQL中实现汉字的拼音首字母查询

由于汉语拼音首字母也就23个,该方法利用汉字字符按拼音字母排序的特点来生成对应的拼单首字母,只需找到这23个汉语拼音首字母中分别排序在第一的汉字生成23条临时表数据用于参照,即可简单实现汉字匹配拼音首字母

CREATE FUNCTION f_GetPyAcronym(@str nvarchar(4000)) 
RETURNS nvarchar(4000) 
AS 
BEGIN 
	declare @strlen int, @re nvarchar(4000) 
	declare @t table(chr nchar(1) collate Chinese_PRC_CI_AS, letter nchar(1))

	insert into @t(chr, letter) 
	select N'吖 ', 'A '   
	union all select N'八 ', 'B '   
	union all select N'嚓 ', 'C '
	union all select N'咑 ', 'D '
	union all select N'妸 ', 'E '   
	union all select N'发 ', 'F '   
	union all select N'旮 ', 'G '   
	union all select N'铪 ', 'H '   
	union all select N'丌 ', 'J '   
	union all select N'咔 ', 'K '   
	union all select N'垃 ', 'L '   
	union all select N'呒 ', 'M '   
	union all select N'拏 ', 'N '   
	union all select N'噢 ', 'O '   
	union all select N'妑 ', 'P '   
	union all select N'七 ', 'Q '   
	union all select N'呥 ', 'R '   
	union all select N'仨 ', 'S '   
	union all select N'他 ', 'T '   
	union all select N'屲 ', 'W '   
	union all select N'夕 ', 'X '   
	union all select N'丫 ', 'Y '   
	union all select N'帀 ', 'Z '	--(注意该字是“帀”【读zā】,不是“币”)

	select @strlen = len(@str), @re = '' 

    while @strlen > 0 
    begin 
        select top 1 @re = letter + @re, @strlen = @strlen - 1 
        from @t a
		where chr <= substring(@str, @strlen, 1)
        order by chr desc 

        if @@rowcount=0
			select @re = substring(@str, @strlen, 1) + @re, @strlen = @strlen - 1	--排在汉字前面的字符(比如英文字母)原样输出
    end 
	
    RETURN(@re) 
END 

测试:

select dbo.f_GetPyAcronym(N'abc- 汉字 -abc')  --得到结果:“abc- HZ -abc”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值