zoukankan      html  css  js  c++  java
  • sql中怎么根据汉字的拼音首字母查询

    --可用,速度可以
    if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[fGetPy]')   and   xtype   in   (N'FN',   N'IF',   N'TF'))   
       drop   function   [dbo].[fGetPy]   
       GO   
         
       --创建取拼音函数  
       create   function   fGetPy(@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>'z'   
       select   @n   =   @n   +1   
       ,@c   =   case   chn   when   @chn   then   char(@n)   else   @c   end   
       from(   
       select   top   27   *   from   (   
       select   chn   =   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   '' --because   have   no   'i'   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   '' --no   'u'   
       union   all   select   '' --no   'v'   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   ''   
       union   all   select   @chn)   as   a   
       order   by   chn   COLLATE   Chinese_PRC_CI_AS     
       )   as   b   
       else   set   @c='a'   
       set   @return=@return+@c   
       end   
       return(@return)   
       end   
         
       go  
    --测试 
    select * from dbo.T_CITY_INFO where dbo.fGetPy(cy_name)='sh'
    
    速度太慢
    --2. 汉字首字母查询处理用户定义函数
    CREATE FUNCTION f_GetPY(@str nvarchar(4000))
    RETURNS nvarchar(4000)
    AS
    BEGIN
        DECLARE @py TABLE(
            ch char(1),
            hz1 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS,
            hz2 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS)
        INSERT @py SELECT 'A',N'',N''
        UNION  ALL SELECT 'B',N'',N'簿'
        UNION  ALL SELECT 'C',N'',N''
        UNION  ALL SELECT 'D',N'',N''
        UNION  ALL SELECT 'E',N'',N''
        UNION  ALL SELECT 'F',N'',N''
        UNION  ALL SELECT 'G',N'',N''
        UNION  ALL SELECT 'H',N'',N''
        UNION  ALL SELECT 'J',N'',N''
        UNION  ALL SELECT 'K',N'',N''
        UNION  ALL SELECT 'L',N'',N''
        UNION  ALL SELECT 'M',N'',N''
        UNION  ALL SELECT 'N',N'',N''
        UNION  ALL SELECT 'O',N'',N''
        UNION  ALL SELECT 'P',N'',N''
        UNION  ALL SELECT 'Q',N'',N''
        UNION  ALL SELECT 'R',N'',N''
        UNION  ALL SELECT 'S',N'',N''
        UNION  ALL SELECT 'T',N'',N''
        UNION  ALL SELECT 'W',N'',N''
        UNION  ALL SELECT 'X',N'',N''
        UNION  ALL SELECT 'Y',N'',N''
        UNION  ALL SELECT 'Z',N'',N''
        DECLARE @i int
        SET @i=PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS,@str)
        WHILE @i>0
            SELECT @str=REPLACE(@str,SUBSTRING(@str,@i,1),ch)
                ,@i=PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS,@str)
            FROM @py
            WHERE SUBSTRING(@str,@i,1) BETWEEN hz1 AND hz2
        RETURN(@str)
    END
    GO
  • 相关阅读:
    JVM系列【2】Class文件结构
    JVM系列【5】JVM常用指令
    JVM系列【4】内存模型
    JVM系列【3】Class文件加载过程
    新编html网页设计从入门到精通 (龙马工作室) pdf扫描版​
    HTML5移动开发即学即用(双色) 王志刚 pdf扫描版​
    HTML5和CSS3实例教程 中文版 高清PDF扫描版
    HTML5+CSS3网站设计教程 (张晓景,胡克) [iso]
    HTML5+CSS3+jQuery Mobile轻松构造APP与移动网站 (陈婉凌) 中文pdf扫描版
    HTML5 Canvas游戏开发实战 PDF扫描版
  • 原文地址:https://www.cnblogs.com/goto/p/2443346.html
Copyright © 2011-2022 走看看