sql函数汉字转拼音

--方法一sqlserver汉字转拼音首字母
--调用方法 select dbo.procGetPY ('中國')
USE [tt]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_GetPinYin]    Script Date: 12/29/2021 20:21:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_GetPinYin] 
( 
  @str NVARCHAR(4000) 
) 

RETURNS NVARCHAR(4000) 
--WITH ENCRYPTION 
AS 
    BEGIN 
    DECLARE @WORD NCHAR(1),@PY NVARCHAR(4000) 

    SET @PY='' 

    WHILE LEN(@STR)>0 
    BEGIN 
        SET @WORD=LEFT(@STR,1) 

        --如果非漢字字符﹐返回原字符 
        SET @PY=@PY+(CASE WHEN UNICODE(@WORD) BETWEEN 19968 AND 19968+20901 
        THEN ( 
        SELECT TOP 1 PY 
        FROM 
        ( 
            SELECT 'A' AS PY,N'' AS WORD 
            UNION ALL SELECT 'B',N'簿' 
            UNION ALL SELECT 'C',N'' 
            UNION ALL SELECT 'D',N'' 
            UNION ALL SELECT 'E',N'' 
            UNION ALL SELECT 'F',N'' 
            UNION ALL SELECT 'G',N'' 
            UNION ALL SELECT 'H',N'' 
            UNION ALL SELECT 'J',N'' 
            UNION ALL SELECT 'K',N'' 
            UNION ALL SELECT 'L',N'' 
            UNION ALL SELECT 'M',N'' 
            UNION ALL SELECT 'N',N'' 
            UNION ALL SELECT 'O',N'' 
            UNION ALL SELECT 'P',N'' 
            UNION ALL SELECT 'Q',N'' 
            UNION ALL SELECT 'R',N'' 
            UNION ALL SELECT 'S',N'' 
            UNION ALL SELECT 'T',N'' 
            UNION ALL SELECT 'W',N'' 
            UNION ALL SELECT 'X',N'' 
            UNION ALL SELECT 'Y',N'' 
            UNION ALL SELECT 'Z',N'' 
        ) T 
        WHERE WORD>=@WORD COLLATE CHINESE_PRC_CS_AS_KS_WS 
        ORDER BY PY ASC 
        ) 
        ELSE @WORD 
        END) 
        SET @STR=RIGHT(@STR,LEN(@STR)-1) 
    END 

    RETURN @PY 

END
-方法二sqlserver汉字转全拼 
--调用方法 select dbo. procGetPinYin ('中國')

USE [tt]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_GetPinYin1]    Script Date: 12/29/2021 20:22:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[fn_GetPinYin1](@str varchar(100))
returns varchar(8000)
as
begin
 declare @re varchar(8000),@crs varchar(10)
 declare @strlen int 
 select @strlen=len(@str),@re=''
 while @strlen>0
 begin  
  set @crs= substring(@str,@strlen,1)
      select @re=
        case
        when @crs<'' then @crs
        when @crs<='' then 'a'
        when @crs<='' then 'ai'
        when @crs<='' then 'an'
        when @crs<='' then 'ang'
        when @crs<='' then 'ao'
        when @crs<='' then 'ba'
        when @crs<='' then 'bai'
        when @crs<='' then 'ban'
        when @crs<='' then 'bang'
        when @crs<='' then 'bao'
        when @crs<='' then 'bei'
        when @crs<='' then 'ben'
        when @crs<='' then 'beng'
        when @crs<='' then 'bi'
        when @crs<='' then 'bian'
        when @crs<='' then 'biao'
        when @crs<='' then 'bie'
        when @crs<='' then 'bin'
        when @crs<='' then 'bing'
        when @crs<='' then 'bo'
        when @crs<='簿' then 'bu'
        when @crs<='' then 'ca'
        when @crs<='' then 'cai'
        when @crs<='' then 'can'
        when @crs<='' then 'cang'
        when @crs<='' then 'cao'
        when @crs<='' then 'ce'
        when @crs<='' then 'cen'
        when @crs<='' then 'ceng'
        when @crs<='' then 'cha'
        when @crs<='' then 'chai'
        when @crs<='' then 'chan'
        when @crs<='' then 'chang'
        when @crs<='' then 'chao'
        when @crs<='' then 'che'
        when @crs<='' then 'chen'
        when @crs<='' then 'cheng'
        when @crs<='' then 'chi'
        when @crs<='' then 'chong'
        when @crs<='' then 'chou'
        when @crs<='' then 'chu'
        when @crs<='' then 'chuai'
        when @crs<='' then 'chuan'
        when @crs<='' then 'chuang'
        when @crs<='' then 'chui'
        when @crs<='' then 'chun'
        when @crs<='' then 'chuo'
        when @crs<='' then 'ci'
        when @crs<='' then 'cong'
        when @crs<='' then 'cou'
        when @crs<='' then 'cu'
        when @crs<='' then 'cuan'
        when @crs<='' then 'cui'
        when @crs<='籿' then 'cun'
        when @crs<='' then 'cuo'
        when @crs<='' then 'da'
        when @crs<='' then 'dai'
        when @crs<='' then 'dan'
        when @crs<='' then 'dang'
        when @crs<='' then 'dao'
        when @crs<='' then 'de'
        when @crs<='' then 'den'
        when @crs<='' then 'deng'
        when @crs<='' then 'di'
        when @crs<='' then 'dia'
        when @crs<='' then 'dian'
        when @crs<='' then 'diao'
        when @crs<='' then 'die'
        when @crs<='' then 'ding'
        when @crs<='' then 'diu'
        when @crs<='' then 'dong'
        when @crs<='' then 'dou'
        when @crs<='' then 'du'
        when @crs<='' then 'duan'
        when @crs<='' then 'dui'
        when @crs<='' then 'dun'
        when @crs<='' then 'duo'
        when @crs<='' then 'e'
        when @crs<='' then 'en'
        when @crs<='' then 'eng'
        when @crs<='' then 'er'
        when @crs<='' then 'fa'
        when @crs<='' then 'fan'
        when @crs<='' then 'fang'
        when @crs<='' then 'fei'
        when @crs<='' then 'fen'
        when @crs<='' then 'feng'
        when @crs<='' then 'fo'
        when @crs<='' then 'fou'
        when @crs<='' then 'fu'
        when @crs<='' then 'ga'
        when @crs<='' then 'gai'
        when @crs<='' then 'gan'
        when @crs<='' then 'gang'
        when @crs<='' then 'gao'
        when @crs<='' then 'ge'
        when @crs<='' then 'gei'
        when @crs<='' then 'gen'
        when @crs<='' then 'geng'
        when @crs<='' then 'gong'
        when @crs<='' then 'gou'
        when @crs<='' then 'gu'
        when @crs<='詿' then 'gua'
        when @crs<='' then 'guai'
        when @crs<='' then 'guan'
        when @crs<='' then 'guang'
        when @crs<='' then 'gui'
        when @crs<='' then 'gun'
        when @crs<='' then 'guo'
        when @crs<='' then 'ha'
        when @crs<='' then 'hai'
        when @crs<='' then 'han'
        when @crs<='' then 'hang'
        when @crs<='' then 'hao'
        when @crs<='' then 'he'
        when @crs<='' then 'hei'
        when @crs<='' then 'hen'
        when @crs<='' then 'heng'
        when @crs<='' then 'hong'
        when @crs<='' then 'hou'
        when @crs<='' then 'hu'
        when @crs<='' then 'hua'
        when @crs<='' then 'huai'
        when @crs<='' then 'huan'
        when @crs<='' then 'huang'
        when @crs<='' then 'hui'
        when @crs<='' then 'hun'
        when @crs<='' then 'huo'
        when @crs<='' then 'ji'
        when @crs<='' then 'jia'
        when @crs<='' then 'jian'
        when @crs<='' then 'jiang'
        when @crs<='' then 'jiao'
        when @crs<='' then 'jie'
        when @crs<='' then 'jin'
        when @crs<='' then 'jing'
        when @crs<='' then 'jiong'
        when @crs<='' then 'jiu'
        when @crs<='' then 'ju'
        when @crs<='' then 'juan'
        when @crs<='' then 'jue'
        when @crs<='' then 'jun'
        when @crs<='' then 'ka'
        when @crs<='' then 'kai'
        when @crs<='' then 'kan'
        when @crs<='' then 'kang'
        when @crs<='' then 'kao'
        when @crs<='' then 'ke'
        when @crs<='' then 'ken'
        when @crs<='' then 'keng'
        when @crs<='' then 'kong'
        when @crs<='' then 'kou'
        when @crs<='' then 'ku'
        when @crs<='' then 'kua'
        when @crs<='' then 'kuai'
        when @crs<='' then 'kuan'
        when @crs<='' then 'kuang'
        when @crs<='' then 'kui'
        when @crs<='' then 'kun'
        when @crs<='' then 'kuo'
        when @crs<='' then 'la'
        when @crs<='' then 'lai'
        when @crs<='' then 'lan'
        when @crs<='' then 'lang'
        when @crs<='' then 'lao'
        when @crs<='' then 'le'
        when @crs<='' then 'lei'
        when @crs<='' then 'leng'
        when @crs<='' then 'li'
        when @crs<='' then 'lia'
        when @crs<='' then 'lian'
        when @crs<='' then 'liang'
        when @crs<='' then 'liao'
        when @crs<='' then 'lie'
        when @crs<='' then 'lin'
        when @crs<='' then 'ling'
        when @crs<='' then 'liu'
        when @crs<='' then 'long'
        when @crs<='' then 'lou'
        when @crs<='' then 'lu'
        when @crs<='' then 'lv'
        when @crs<='' then 'luan'
        when @crs<='' then 'lue'
        when @crs<='' then 'lun'
        when @crs<='' then 'luo'
        when @crs<='' then 'ma'
        when @crs<='' then 'mai'
        when @crs<='' then 'man'
        when @crs<='' then 'mang'
        when @crs<='' then 'mao'
        when @crs<='' then 'me'
        when @crs<='' then 'mei'
        when @crs<='' then 'men'
        when @crs<='' then 'meng'
        when @crs<='' then 'mi'
        when @crs<='' then 'mian'
        when @crs<='' then 'miao'
        when @crs<='' then 'mie'
        when @crs<='' then 'min'
        when @crs<='' then 'ming'
        when @crs<='' then 'miu'
        when @crs<='' then 'mo'
        when @crs<='' then 'mou'
        when @crs<='' then 'mu'
        when @crs<='' then 'na'
        when @crs<='' then 'nai'
        when @crs<='' then 'nan'
        when @crs<='' then 'nang'
        when @crs<='' then 'nao'
        when @crs<='' then 'ne'
        when @crs<='' then 'nei'
        when @crs<='' then 'nen'
        when @crs<='' then 'neng'
        when @crs<='' then 'ni'
        when @crs<='' then 'nian'
        when @crs<='' then 'niang'
        when @crs<='' then 'niao'
        when @crs<='' then 'nie'
        when @crs<='' then 'nin'
        when @crs<='' then 'ning'
        when @crs<='' then 'niu'
        when @crs<='' then 'nong'
        when @crs<='' then 'nou'
        when @crs<='' then 'nu'
        when @crs<='' then 'nv'
        when @crs<='' then 'nue'
        when @crs<='' then 'nuan'
        when @crs<='' then 'nuo'
        when @crs<='' then 'o'
        when @crs<='' then 'ou'
        when @crs<='' then 'pa'
        when @crs<='' then 'pai'
        when @crs<='' then 'pan'
        when @crs<='' then 'pang'
        when @crs<='' then 'pao'
        when @crs<='' then 'pei'
        when @crs<='' then 'pen'
        when @crs<='' then 'peng'
        when @crs<='' then 'pi'
        when @crs<='' then 'pian'
        when @crs<='' then 'piao'
        when @crs<='' then 'pie'
        when @crs<='' then 'pin'
        when @crs<='' then 'ping'
        when @crs<='' then 'po'
        when @crs<='' then 'pou'
        when @crs<='' then 'pu'
        when @crs<='' then 'qi'
        when @crs<='' then 'qia'
        when @crs<='' then 'qian'
        when @crs<='' then 'qiang'
        when @crs<='' then 'qiao'
        when @crs<='' then 'qie'
        when @crs<='' then 'qin'
        when @crs<='' then 'qing'
        when @crs<='' then 'qiong'
        when @crs<='' then 'qiu'
        when @crs<='' then 'qu'
        when @crs<='' then 'quan'
        when @crs<='' then 'que'
        when @crs<='' then 'qun'
        when @crs<='' then 'ran'
        when @crs<='' then 'rang'
        when @crs<='' then 'rao'
        when @crs<='' then 're'
        when @crs<='' then 'ren'
        when @crs<='' then 'reng'
        when @crs<='' then 'ri'
        when @crs<='' then 'rong'
        when @crs<='嶿' then 'rou'
        when @crs<='' then 'ru'
        when @crs<='' then 'ruan'
        when @crs<='' then 'rui'
        when @crs<='' then 'run'
        when @crs<='' then 'ruo'
        when @crs<='' then 'sa'
        when @crs<='' then 'sai'
        when @crs<='' then 'san'
        when @crs<='' then 'sang'
        when @crs<='' then 'sao'
        when @crs<='' then 'se'
        when @crs<='' then 'sen'
        when @crs<='' then 'seng'
        when @crs<='' then 'sha'
        when @crs<='' then 'shai'
        when @crs<='' then 'shan'
        when @crs<='' then 'shang'
        when @crs<='' then 'shao'
        when @crs<='' then 'she'
        when @crs<='' then 'shen'
        when @crs<='' then 'sheng'
        when @crs<='' then 'shi'
        when @crs<='' then 'shou'
        when @crs<='' then 'shu'
        when @crs<='' then 'shua'
        when @crs<='' then 'shuai'
        when @crs<='' then 'shuan'
        when @crs<='' then 'shuang'
        when @crs<='' then 'shui'
        when @crs<='' then 'shun'
        when @crs<='' then 'shuo'
        when @crs<='' then 'si'
        when @crs<='' then 'song'
        when @crs<='' then 'sou'
        when @crs<='' then 'su'
        when @crs<='' then 'suan'
        when @crs<='' then 'sui'
        when @crs<='' then 'sun'
        when @crs<='' then 'suo'
        when @crs<='' then 'ta'
        when @crs<='' then 'tai'
        when @crs<='' then 'tan'
        when @crs<='' then 'tang'
        when @crs<='' then 'tao'
        when @crs<='' then 'te'
        when @crs<='' then 'teng'
        when @crs<='' then 'ti'
        when @crs<='' then 'tian'
        when @crs<='' then 'tiao'
        when @crs<='' then 'tie'
        when @crs<='' then 'ting'
        when @crs<='' then 'tong'
        when @crs<='' then 'tou'
        when @crs<='' then 'tu'
        when @crs<='' then 'tuan'
        when @crs<='' then 'tui'
        when @crs<='' then 'tun'
        when @crs<='' then 'tuo'
        when @crs<='' then 'wa'
        when @crs<='' then 'wai'
        when @crs<='' then 'wan'
        when @crs<='' then 'wang'
        when @crs<='' then 'wei'
        when @crs<='' then 'wen'
        when @crs<='' then 'weng'
        when @crs<='' then 'wo'
        when @crs<='' then 'wu'
        when @crs<='' then 'xi'
        when @crs<='' then 'xia'
        when @crs<='' then 'xian'
        when @crs<='' then 'xiang'
        when @crs<='' then 'xiao'
        when @crs<='' then 'xie'
        when @crs<='' then 'xin'
        when @crs<='' then 'xing'
        when @crs<='' then 'xiong'
        when @crs<='' then 'xiu'
        when @crs<='' then 'xu'
        when @crs<='' then 'xuan'
        when @crs<='' then 'xue'
        when @crs<='' then 'xun'
        when @crs<='' then 'ya'
        when @crs<='' then 'yan'
        when @crs<='' then 'yang'
        when @crs<='' then 'yao'
        when @crs<='' then 'ye'
        when @crs<='' then 'yi'
        when @crs<='' then 'yin'
        when @crs<='' then 'ying'
        when @crs<='' then 'yo'
        when @crs<='' then 'yong'
        when @crs<='' then 'you'
        when @crs<='' then 'yu'
        when @crs<='' then 'yuan'
        when @crs<='' then 'yue'
        when @crs<='' then 'yun'
        when @crs<='' then 'za'
        when @crs<='' then 'zai'
        when @crs<='' then 'zan'
        when @crs<='' then 'zang'
        when @crs<='' then 'zao'
        when @crs<='' then 'ze'
        when @crs<='' then 'zei'
        when @crs<='' then 'zen'
        when @crs<='' then 'zeng'
        when @crs<='' then 'zha'
        when @crs<='' then 'zhai'
        when @crs<='' then 'zhan'
        when @crs<='' then 'zhang'
        when @crs<='' then 'zhao'
        when @crs<='' then 'zhe'
        when @crs<='' then 'zhen'
        when @crs<='' then 'zheng'
        when @crs<='' then 'zhi'
        when @crs<='' then 'zhong'
        when @crs<='' then 'zhou'
        when @crs<='' then 'zhu'
        when @crs<='' then 'zhua'
        when @crs<='' then 'zhuai'
        when @crs<='' then 'zhuan'
        when @crs<='' then 'zhuang'
        when @crs<='' then 'zhui'
        when @crs<='' then 'zhun'
        when @crs<='' then 'zhuo'
        when @crs<='' then 'zi'
        when @crs<='' then 'zong'
        when @crs<='' then 'zou'
        when @crs<='' then 'zu'
        when @crs<='' then 'zuan'
        when @crs<='' then 'zui'
        when @crs<='' then 'zun'
        when @crs<='' then 'zuo'
        --else  @crs end+' '+@re,@strlen=@strlen-1 
        --去掉拼音之间的间隔
        else  @crs end+''+@re,@strlen=@strlen-1 
   end
 return(@re)
end
原文地址:https://www.cnblogs.com/lgx5/p/15746626.html