11人参与 • 2025-06-09 • MsSqlserver
java的同学可以了解下pinyin4j,js可以了解chinesepy.js
获取拼音的意义多在于需对中文进行排序,例如:通讯录。
获取汉字首字母的解决方案有
第二个方法不太好用有的字获取不出来,这个比较顶
alter function [dbo].[getpy](@str varchar(500)) returns varchar(500) as begin declare @word nchar(1),@py nvarchar(4000) set @py='' 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
把@length=1改成@length=1len(@str) ,效果从输入首字的首字母变成输出全部字的首字母
create function [dbo].[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=1--len(@str)--输入汉字的长度 只需要返回首字母 set @str1=''--用于存放返回值 while @cyc<=@length begin select @charcate=cast(substring(@str,@cyc,1) as varbinary)--每次取出一个字并将其转变成二进制,便于与gbk编码表进行比较 if @str like '[a-z]%' --英文开头直接返回 return @str 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 set @str1=@str1+'g' else if @charcate>=0xb9fe and @charcate<=0xbbf6 set @str1=@str1+'h' else if @charcate>=0xbbf7 and @charcate<=0xbfa5 set @str1=@str1+'i' else if @charcate>=0xbfa6 and @charcate<=0xc0ab set @str1=@str1+'j' else if @charcate>=0xc0ac and @charcate<=0xc2e7 set @str1=@str1+'k' else if @charcate>=0xc2e8 and @charcate<=0xc4c2 set @str1=@str1+'l' else if @charcate>=0xc2e8 and @charcate<=0xc4c2 set @str1=@str1+'m' else if @charcate>=0xc4c3 and @charcate<=0xc5b5 set @str1=@str1+'n' else if @charcate>=0xc5b6 and @charcate<=0xc5bd set @str1=@str1+'o' else if @charcate>=0xc5be and @charcate<=0xc6d9 set @str1=@str1+'p' else if @charcate>=0xc6da and @charcate<=0xc8ba set @str1=@str1+'q' else if @charcate>=0xc8bb and @charcate<=0xc8f5 set @str1=@str1+'r' else if @charcate>=0xc8f6 and @charcate<=0xcbf9 set @str1=@str1+'s' else if @charcate>=0xcbfa and @charcate<=0xcdd9 set @str1=@str1+'t' else if @charcate>=0xcdda and @charcate<=0xcef3 set @str1=@str1+'w' else if @charcate>=0xcef4 and @charcate<=0xd1b8 set @str1=@str1+'x' else if @charcate>=0xd1b9 and @charcate<=0xd4d0 set @str1=@str1+'y' else if @charcate>=0xd4d1 and @charcate<=0xd7f9 set @str1=@str1+'z' set @cyc=@cyc+1--取出输入汉字的下一个字 end return @str1--返回输入汉字的首字母 end
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论