按拼音首字母查询(mssql)

1. 先创建function

create function   f_GetPy(@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   '吖 ', 'A '   union   all   select   '八 ', 'B '   union   all 
    select   '嚓 ', 'C '   union   all   select   '咑 ', 'D '   union   all 
    select   '妸 ', 'E '   union   all   select   '发 ', 'F '   union   all 
    select   '旮 ', 'G '   union   all   select   '铪 ', 'H '   union   all 
    select   '丌 ', 'J '   union   all   select   '咔 ', 'K '   union   all 
    select   '垃 ', 'L '   union   all   select   '嘸 ', 'M '   union   all 
    select   '拏 ', 'N '   union   all   select   '噢 ', 'O '   union   all 
    select   '妑 ', 'P '   union   all   select   '七 ', 'Q '   union   all 
    select   '呥 ', 'R '   union   all   select   '仨 ', 'S '   union   all 
    select   '他 ', 'T '   union   all   select   '屲 ', 'W '   union   all 
    select   '夕 ', 'X '   union   all   select   '丫 ', 'Y '   union   all 
    select   '帀 ', '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 

2. 通过调用该function即可.
select dbo.f_GetPy('我')---->W

select dbo.f_GetPy('你')---->N

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用SQL Server中的内置函数SUBSTRING和CHARINDEX来实现这个功能。首先,你需要获取所有数据的拼音字母,并将其存储在一个新的列中。可以使用PHP中的拼音转换函数来实现这一点。例如,使用如下代码将一个字符串转换为拼音字母: ``` function getFirstChar($str){ if(empty($str)){ return ''; } $fchar = ord($str{0}); if($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0}); $s1 = iconv('UTF-8', 'gb2312', $str); $s2 = iconv('gb2312', 'UTF-8', $s1); $s = $s2 == $str ? $s1 : $str; $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if($asc >= -20319 && $asc <= -20284) return 'A'; if($asc >= -20283 && $asc <= -19776) return 'B'; if($asc >= -19775 && $asc <= -19219) return 'C'; if($asc >= -19218 && $asc <= -18711) return 'D'; if($asc >= -18710 && $asc <= -18527) return 'E'; if($asc >= -18526 && $asc <= -18240) return 'F'; if($asc >= -18239 && $asc <= -17923) return 'G'; if($asc >= -17922 && $asc <= -17418) return 'H'; if($asc >= -17417 && $asc <= -16475) return 'J'; if($asc >= -16474 && $asc <= -16213) return 'K'; if($asc >= -16212 && $asc <= -15641) return 'L'; if($asc >= -15640 && $asc <= -15166) return 'M'; if($asc >= -15165 && $asc <= -14923) return 'N'; if($asc >= -14922 && $asc <= -14915) return 'O'; if($asc >= -14914 && $asc <= -14631) return 'P'; if($asc >= -14630 && $asc <= -14150) return 'Q'; if($asc >= -14149 && $asc <= -14091) return 'R'; if($asc >= -14090 && $asc <= -13319) return 'S'; if($asc >= -13318 && $asc <= -12839) return 'T'; if($asc >= -12838 && $asc <= -12557) return 'W'; if($asc >= -12556 && $asc <= -11848) return 'X'; if($asc >= -11847 && $asc <= -11056) return 'Y'; if($asc >= -11055 && $asc <= -10247) return 'Z'; return null; } ``` 然后,你可以使用以下SQL语句来创建一个新列并填充它: ``` ALTER TABLE your_table ADD py_initials VARCHAR(10); UPDATE your_table SET py_initials = SUBSTRING((SELECT ',' + dbo.get_first_char(name) FROM your_table WHERE id = t.id FOR XML PATH('')),2,8000) ``` 这将在你的表中创建一个名为py_initials的新列,并将每个条目的拼音字母存储在该列中。 接下来,你可以使用以下SQL查询来检索指定拼音字母的所有数据: ``` SELECT * FROM your_table WHERE CHARINDEX('A', py_initials) = 1 ``` 这将返回所有拼音字母为A的数据。你可以将A替换为任何其他拼音字母来搜索不同的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值