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
  • 相关阅读:
    MySQL学习——SQL查询语句(连接查询&子查询)(三)
    MySQL学习——SQL查询语句(使用集合函数查询)(二)
    MySQL学习——SQL查询语句(一)
    MySQL学习——操作数据库(增删改查)(二)
    MySQL学习——操作数据库(增删改查)(一)
    MySQL学习——MySQL数据库概述与基础
    Python学习之——Http协议简介
    Python学习之——Socket套接字(UDP连接)
    第04组 Beta版本演示
    第04组 Beta冲刺(4/5)
  • 原文地址:https://www.cnblogs.com/goto/p/2443346.html
Copyright © 2011-2022 走看看