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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM