首頁  >  文章  >  資料庫  >  一個效率很高的漢字轉拼音首字母的函數

一個效率很高的漢字轉拼音首字母的函數

大家讲道理
大家讲道理原創
2016-11-10 11:22:021895瀏覽

場景:

需要透過使用者輸入的姓名關鍵字來搜尋使用者。使用者輸入關鍵字'x'來搜尋使用者(資料來自表[Name欄位中]或記憶體[List]中)

要求:

所得到的結果排序應為:

  • x

  • x

  • x

x

xia


🎜xiao🎜🎜🎜🎜yx🎜🎜🎜🎜即:🎜🎜包含x字母的結果均應顯示出來🎜🎜即:🎜🎜包含x字母的結果均應顯示出來🎜🎜。的前提下較短的結果應排在前面(如x排在xia前面)🎜
create function [dbo].[fn_getpy2](@Str varchar(500)='')
returns varchar(500)
as
begin
 declare @strlen int,@return varchar(500),@ii int
 declare @n int,@c char(1),@chn nchar(1)

 select @strlen=len(@str),@return='',@ii=0
 set @ii=0
 while @ii<@strlen
 begin
  select @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)
  if @chn>&#39;z&#39;
  select @n = @n +1
     ,@c = case chn when @chn then char(@n) else @c end
   from(
    select top 27 * from (
     select chn = &#39;吖&#39;
     union all select &#39;八&#39;
     union all select &#39;嚓&#39;
     union all select &#39;咑&#39;
     union all select &#39;妸&#39;
     union all select &#39;发&#39;
     union all select &#39;旮&#39;
     union all select &#39;铪&#39;
     union all select &#39;丌&#39;  --because have no &#39;i&#39;
     union all select &#39;丌&#39;
     union all select &#39;咔&#39;
     union all select &#39;垃&#39;
     union all select &#39;嘸&#39;
     union all select &#39;拏&#39;
     union all select &#39;噢&#39;
     union all select &#39;妑&#39;
     union all select &#39;七&#39;
     union all select &#39;呥&#39;
     union all select &#39;仨&#39;
     union all select &#39;他&#39;
     union all select &#39;屲&#39;  --no &#39;u&#39;
     union all select &#39;屲&#39;  --no &#39;v&#39;
     union all select &#39;屲&#39;
     union all select &#39;夕&#39;
     union all select &#39;丫&#39;
     union all select &#39;帀&#39;
     union all select @chn) as a
    order by chn COLLATE Chinese_PRC_CI_AS 
   ) as b
  else set @c=@chn
  set @return=@return+@c
 end
 return(@return)
end
🎜🎜🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn