sql server 中的汉字转化为拼音

时间:2021-02-17 06:40:51

在这里转化汉字有两种,一种是只取汉字的首字母另一种是取汉字胡全拼,

1 只取汉字的首字母 代码如下:

sql server 中的汉字转化为拼音sql server 中的汉字转化为拼音View Code
SET  ANSI_NULLS ON
SET  QUOTED_IDENTIFIER ON
go

CREATE   FUNCTION  [dbo].[fun_getPY](@str nvarchar(4000) ) 
RETURNS  nvarchar(4000) 
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 

GO

SELECT dbo.fun_getPY('来1吧')

 运行结果如下:

sql server 中的汉字转化为拼音

2 另一种是取汉字胡全拼 代码如下:

sql server 中的汉字转化为拼音sql server 中的汉字转化为拼音View Code
CREATE function [dbo].[fn_GetPinyin](@words nVARCHAR(2000))   
RETURNS VARCHAR(8000)   
AS   

BEGIN 
    DECLARE @word nchar(1) 
    DECLARE @pinyin VARCHAR(8000) 
    DECLARE @i INT
    DECLARE @words_len INT
    DECLARE @unicode INT
    SET @i = 1 
    SET @words =ltrim(rtrim(@words)) 
    SET @words_len =len(@words) 
    WHILE(@i <= @words_len)--循环取字符
    BEGIN 
    SET @word =substring(@words, @i, 1) 
    SET @unicode =unicode(@word) 
    SET @pinyin =ISNULL(@pinyin +SPACE(1),'')+ (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 'ai',N''
    UNION ALL SELECT'an',N''
    UNION ALL SELECT'ang',N''
    UNION ALL SELECT'ao',N''
    UNION ALL SELECT'ba',N''
    UNION ALL SELECT'bai',N''--韛兡瓸
    UNION ALL SELECT'ban',N''
    UNION ALL SELECT'bang',N''
    UNION ALL SELECT'bao',N''
    UNION ALL SELECT'bei',N''
    UNION ALL SELECT'ben',N''
    UNION ALL SELECT'beng',N''
    UNION ALL SELECT'bi',N''
    UNION ALL SELECT'bian',N''
    UNION ALL SELECT'biao',N''
    UNION ALL SELECT'bie',N''
    UNION ALL SELECT'bin',N''
    UNION ALL SELECT'bing',N''
    UNION ALL SELECT'bo',N''
    UNION ALL SELECT'bu',N'簿'
    UNION ALL SELECT'ca',N''
    UNION ALL SELECT'cai',N''--縩乲
    UNION ALL SELECT'can',N''
    UNION ALL SELECT'cang',N''
    UNION ALL SELECT'cao',N''
    UNION ALL SELECT'ce',N''
    UNION ALL SELECT'cen',N''
    UNION ALL SELECT'ceng',N''--硛硳岾猠乽
    UNION ALL SELECT'cha',N''
    UNION ALL SELECT'chai',N''
    UNION ALL SELECT'chan',N''
    UNION ALL SELECT'chang',N''
    UNION ALL SELECT'chao',N''
    UNION ALL SELECT'che',N''
    UNION ALL SELECT'chen',N''
    UNION ALL SELECT'cheng',N''
    UNION ALL SELECT'chi',N''
    UNION ALL SELECT'chong',N''
    UNION ALL SELECT'chou',N''
    UNION ALL SELECT'chu',N''
    UNION ALL SELECT'chuai',N''
    UNION ALL SELECT'chuan',N''
    UNION ALL SELECT'chuang',N''
    UNION ALL SELECT'chui',N''
    UNION ALL SELECT'chun',N''
    UNION ALL SELECT'chuo',N''
    UNION ALL SELECT'ci',N''--賜嗭
    UNION ALL SELECT'cong',N''
    UNION ALL SELECT'cou',N''
    UNION ALL SELECT'cu',N''
    UNION ALL SELECT'cuan',N''
    UNION ALL SELECT'cui',N''
    UNION ALL SELECT'cun',N'籿'
    UNION ALL SELECT'cuo',N''
    UNION ALL SELECT'da',N''
    UNION ALL SELECT'dai',N''
    UNION ALL SELECT'dan',N''
    UNION ALL SELECT'dang',N''
    UNION ALL SELECT'dao',N''
    UNION ALL SELECT'de',N''
    UNION ALL SELECT'den',N''
    UNION ALL SELECT'deng',N''
    UNION ALL SELECT'di',N''
    UNION ALL SELECT'dia',N''
    UNION ALL SELECT'dian',N''
    UNION ALL SELECT'diao',N''
    UNION ALL SELECT'die',N''--眰嚸
    UNION ALL SELECT'ding',N''
    UNION ALL SELECT'diu',N''
    UNION ALL SELECT'dong',N''
    UNION ALL SELECT'dou',N''
    UNION ALL SELECT'du',N''
    UNION ALL SELECT'duan',N''--籪叾
    UNION ALL SELECT'dui',N''
    UNION ALL SELECT'dun',N''
    UNION ALL SELECT'duo',N''
    UNION ALL SELECT'e',N''
    UNION ALL SELECT'en',N''
    UNION ALL SELECT'eng',N''
    UNION ALL SELECT'er',N''
    UNION ALL SELECT'fa',N''
    UNION ALL SELECT'fan',N''
    UNION ALL SELECT'fang',N''
    UNION ALL SELECT'fei',N''
    UNION ALL SELECT'fen',N''
    UNION ALL SELECT'feng',N''
    UNION ALL SELECT'fo',N''
    UNION ALL SELECT'fou',N''
    UNION ALL SELECT'fu',N''--鰒猤
    UNION ALL SELECT'ga',N''
    UNION ALL SELECT'gai',N''
    UNION ALL SELECT'gan',N''
    UNION ALL SELECT'gang',N''
    UNION ALL SELECT'gao',N''
    UNION ALL SELECT'ge',N''
    UNION ALL SELECT'gei',N''
    UNION ALL SELECT'gen',N''
    UNION ALL SELECT'geng',N''--亙堩啹喼嗰
    UNION ALL SELECT'gong',N''--熕贑兝兣
    UNION ALL SELECT'gou',N''
    UNION ALL SELECT'gu',N''
    UNION ALL SELECT'gua',N'詿'
    UNION ALL SELECT'guai',N''
    UNION ALL SELECT'guan',N''
    UNION ALL SELECT'guang',N''
    UNION ALL SELECT'gui',N''
    UNION ALL SELECT'gun',N''
    UNION ALL SELECT'guo',N''
    UNION ALL SELECT'ha',N''
    UNION ALL SELECT'hai',N''
    UNION ALL SELECT'han',N''
    UNION ALL SELECT'hang',N''
    UNION ALL SELECT'hao',N''
    UNION ALL SELECT'he',N''
    UNION ALL SELECT'hei',N''
    UNION ALL SELECT'hen',N''
    UNION ALL SELECT'heng',N''--堼囍
    UNION ALL SELECT'hong',N''
    UNION ALL SELECT'hou',N'*'
    UNION ALL SELECT'hu',N'*'
    UNION ALL SELECT'hua',N''
    UNION ALL SELECT'huai',N''
    UNION ALL SELECT'huan',N''
    UNION ALL SELECT'huang',N''
    UNION ALL SELECT'hui',N''
    UNION ALL SELECT'hun',N''
    UNION ALL SELECT'huo',N''
    UNION ALL SELECT'ji',N''
    UNION ALL SELECT'jia',N''
    UNION ALL SELECT'jian',N''
    UNION ALL SELECT'jiang',N''
    UNION ALL SELECT'jiao',N''
    UNION ALL SELECT'jie',N''
    UNION ALL SELECT'jin',N''
    UNION ALL SELECT'jing',N''
    UNION ALL SELECT'jiong',N''
    UNION ALL SELECT'jiu',N''
    UNION ALL SELECT'ju',N''
    UNION ALL SELECT'juan',N''
    UNION ALL SELECT'jue',N''
    UNION ALL SELECT'jun',N''
    UNION ALL SELECT'ka',N''
    UNION ALL SELECT'kai',N''--鎎乫
    UNION ALL SELECT'kan',N''
    UNION ALL SELECT'kang',N''
    UNION ALL SELECT'kao',N''
    UNION ALL SELECT'ke',N''
    UNION ALL SELECT'ken',N''
    UNION ALL SELECT'keng',N''--巪乬唟厼怾
    UNION ALL SELECT'kong',N''
    UNION ALL SELECT'kou',N''
    UNION ALL SELECT'ku',N''
    UNION ALL SELECT'kua',N''
    UNION ALL SELECT'kuai',N''
    UNION ALL SELECT'kuan',N''
    UNION ALL SELECT'kuang',N''
    UNION ALL SELECT'kui',N''
    UNION ALL SELECT'kun',N''
    UNION ALL SELECT'kuo',N''
    UNION ALL SELECT'la',N''
    UNION ALL SELECT'lai',N''
    UNION ALL SELECT'lan',N''
    UNION ALL SELECT'lang',N''
    UNION ALL SELECT'lao',N''
    UNION ALL SELECT'le',N''
    UNION ALL SELECT'lei',N''--嘞脷
    UNION ALL SELECT'leng',N''
    UNION ALL SELECT'li',N''
    UNION ALL SELECT'lia',N''
    UNION ALL SELECT'lian',N''
    UNION ALL SELECT'liang',N''
    UNION ALL SELECT'liao',N''
    UNION ALL SELECT'lie',N''
    UNION ALL SELECT'lin',N''--轥拎
    UNION ALL SELECT'ling',N''
    UNION ALL SELECT'liu',N''--瓼甅囖咯
    UNION ALL SELECT'long',N''
    UNION ALL SELECT'lou',N''
    UNION ALL SELECT'lu',N''
    UNION ALL SELECT'lv',N''
    UNION ALL SELECT'luan',N''
    UNION ALL SELECT'lue',N''
    UNION ALL SELECT'lun',N''
    UNION ALL SELECT'luo',N''
    UNION ALL SELECT'ma',N''
    UNION ALL SELECT'mai',N''
    UNION ALL SELECT'man',N''
    UNION ALL SELECT'mang',N''
    UNION ALL SELECT'mao',N''
    UNION ALL SELECT'me',N''--癦呅
    UNION ALL SELECT'mei',N''
    UNION ALL SELECT'men',N''
    UNION ALL SELECT'meng',N''--霿踎
    UNION ALL SELECT'mi',N''
    UNION ALL SELECT'mian',N''
    UNION ALL SELECT'miao',N''
    UNION ALL SELECT'mie',N''--鱴瓱
    UNION ALL SELECT'min',N''
    UNION ALL SELECT'ming',N''
    UNION ALL SELECT'miu',N''
    UNION ALL SELECT'mo',N''--耱乮
    UNION ALL SELECT'mou',N''--麰蟱
    UNION ALL SELECT'mu',N''
    UNION ALL SELECT'na',N''
    UNION ALL SELECT'nai',N''
    UNION ALL SELECT'nan',N''
    UNION ALL SELECT'nang',N''
    UNION ALL SELECT'nao',N''
    UNION ALL SELECT'ne',N''
    UNION ALL SELECT'nei',N''--嫩焾
    UNION ALL SELECT'nen',N''
    UNION ALL SELECT'neng',N''--莻嗯鈪銰啱
    UNION ALL SELECT'ni',N''
    UNION ALL SELECT'nian',N''
    UNION ALL SELECT'niang',N''
    UNION ALL SELECT'niao',N''
    UNION ALL SELECT'nie',N''
    UNION ALL SELECT'nin',N''
    UNION ALL SELECT'ning',N''
    UNION ALL SELECT'niu',N''
    UNION ALL SELECT'nong',N''
    UNION ALL SELECT'nou',N''
    UNION ALL SELECT'nu',N''
    UNION ALL SELECT'nv',N''
    UNION ALL SELECT'nue',N''
    UNION ALL SELECT'nuan',N''--硸黁燶郍
    UNION ALL SELECT'nuo',N''
    UNION ALL SELECT'o',N''--毮夞乯鞰
    UNION ALL SELECT'ou',N''
    UNION ALL SELECT'pa',N''
    UNION ALL SELECT'pai',N''--鎃磗
    UNION ALL SELECT'pan',N''
    UNION ALL SELECT'pang',N''
    UNION ALL SELECT'pao',N''
    UNION ALL SELECT'pei',N''
    UNION ALL SELECT'pen',N''
    UNION ALL SELECT'peng',N''--浌巼闏乶喸
    UNION ALL SELECT'pi',N''
    UNION ALL SELECT'pian',N''
    UNION ALL SELECT'piao',N''
    UNION ALL SELECT'pie',N''
    UNION ALL SELECT'pin',N''
    UNION ALL SELECT'ping',N''
    UNION ALL SELECT'po',N''
    UNION ALL SELECT'pou',N''--兺哛
    UNION ALL SELECT'pu',N''
    UNION ALL SELECT'qi',N''
    UNION ALL SELECT'qia',N''
    UNION ALL SELECT'qian',N''
    UNION ALL SELECT'qiang',N''--羻兛瓩
    UNION ALL SELECT'qiao',N''
    UNION ALL SELECT'qie',N''
    UNION ALL SELECT'qin',N''
    UNION ALL SELECT'qing',N''
    UNION ALL SELECT'qiong',N''
    UNION ALL SELECT'qiu',N''
    UNION ALL SELECT'qu',N''
    UNION ALL SELECT'quan',N''
    UNION ALL SELECT'que',N''
    UNION ALL SELECT'qun',N''
    UNION ALL SELECT'ran',N''
    UNION ALL SELECT'rang',N''
    UNION ALL SELECT'rao',N''
    UNION ALL SELECT're',N''
    UNION ALL SELECT'ren',N''
    UNION ALL SELECT'reng',N''
    UNION ALL SELECT'ri',N''
    UNION ALL SELECT'rong',N''
    UNION ALL SELECT'rou',N'嶿'
    UNION ALL SELECT'ru',N''
    UNION ALL SELECT'ruan',N''
    UNION ALL SELECT'rui',N''
    UNION ALL SELECT'run',N''--橍挼
    UNION ALL SELECT'ruo',N''
    UNION ALL SELECT'sa',N''--櫒栍
    UNION ALL SELECT'sai',N''--簺虄
    UNION ALL SELECT'san',N''
    UNION ALL SELECT'sang',N''
    UNION ALL SELECT'sao',N''
    UNION ALL SELECT'se',N''--裇聓
    UNION ALL SELECT'sen',N''
    UNION ALL SELECT'seng',N''--閪縇
    UNION ALL SELECT'sha',N''
    UNION ALL SELECT'shai',N''
    UNION ALL SELECT'shan',N''
    UNION ALL SELECT'shang',N''
    UNION ALL SELECT'shao',N''
    UNION ALL SELECT'she',N''
    UNION ALL SELECT'shen',N''
    UNION ALL SELECT'sheng',N''
    UNION ALL SELECT'shi',N''--鰘齛兙瓧
    UNION ALL SELECT'shou',N''
    UNION ALL SELECT'shu',N''
    UNION ALL SELECT'shua',N''
    UNION ALL SELECT'shuai',N''
    UNION ALL SELECT'shuan',N''
    UNION ALL SELECT'shuang',N''
    UNION ALL SELECT'shui',N''
    UNION ALL SELECT'shun',N''
    UNION ALL SELECT'shuo',N''
    UNION ALL SELECT'si',N''--瀃螦乺
    UNION ALL SELECT'song',N''
    UNION ALL SELECT'sou',N''
    UNION ALL SELECT'su',N''
    UNION ALL SELECT'suan',N''
    UNION ALL SELECT'sui',N''
    UNION ALL SELECT'sun',N''
    UNION ALL SELECT'suo',N''
    UNION ALL SELECT'ta',N''--躢襨
    UNION ALL SELECT'tai',N''
    UNION ALL SELECT'tan',N''
    UNION ALL SELECT'tang',N''
    UNION ALL SELECT'tao',N''--討畓
    UNION ALL SELECT'te',N''
    UNION ALL SELECT'teng',N''--霯唞朰
    UNION ALL SELECT'ti',N''
    UNION ALL SELECT'tian',N''
    UNION ALL SELECT'tiao',N''
    UNION ALL SELECT'tie',N''
    UNION ALL SELECT'ting',N''--濎乭
    UNION ALL SELECT'tong',N''
    UNION ALL SELECT'tou',N''
    UNION ALL SELECT'tu',N''
    UNION ALL SELECT'tuan',N''
    UNION ALL SELECT'tui',N''
    UNION ALL SELECT'tun',N''
    UNION ALL SELECT'tuo',N''
    UNION ALL SELECT'wa',N''
    UNION ALL SELECT'wai',N''
    UNION ALL SELECT'wan',N''
    UNION ALL SELECT'wang',N''
    UNION ALL SELECT'wei',N''
    UNION ALL SELECT'wen',N''
    UNION ALL SELECT'weng',N''
    UNION ALL SELECT'wo',N''
    UNION ALL SELECT'wu',N''
    UNION ALL SELECT'xi',N''
    UNION ALL SELECT'xia',N''
    UNION ALL SELECT'xian',N''
    UNION ALL SELECT'xiang',N''
    UNION ALL SELECT'xiao',N''
    UNION ALL SELECT'xie',N''
    UNION ALL SELECT'xin',N''
    UNION ALL SELECT'xing',N''
    UNION ALL SELECT'xiong',N''
    UNION ALL SELECT'xiu',N''
    UNION ALL SELECT'xu',N''
    UNION ALL SELECT'xuan',N''
    UNION ALL SELECT'xue',N''
    UNION ALL SELECT'xun',N''
    UNION ALL SELECT'ya',N''
    UNION ALL SELECT'yan',N''
    UNION ALL SELECT'yang',N''
    UNION ALL SELECT'yao',N''
    UNION ALL SELECT'ye',N''--鸈膶岃
    UNION ALL SELECT'yi',N''
    UNION ALL SELECT'yin',N''
    UNION ALL SELECT'ying',N''
    UNION ALL SELECT'yo',N''
    UNION ALL SELECT'yong',N''
    UNION ALL SELECT'you',N''
    UNION ALL SELECT'yu',N''
    UNION ALL SELECT'yuan',N''
    UNION ALL SELECT'yue',N''
    UNION ALL SELECT'yun',N''
    UNION ALL SELECT'za',N''
    UNION ALL SELECT'zai',N''
    UNION ALL SELECT'zan',N''
    UNION ALL SELECT'zang',N''
    UNION ALL SELECT'zao',N''
    UNION ALL SELECT'ze',N''
    UNION ALL SELECT'zei',N''
    UNION ALL SELECT'zen',N''
    UNION ALL SELECT'zeng',N''
    UNION ALL SELECT'zha',N''
    UNION ALL SELECT'zhai',N''
    UNION ALL SELECT'zhan',N''
    UNION ALL SELECT'zhang',N''
    UNION ALL SELECT'zhao',N''
    UNION ALL SELECT'zhe',N''
    UNION ALL SELECT'zhen',N''
    UNION ALL SELECT'zheng',N''
    UNION ALL SELECT'zhi',N''
    UNION ALL SELECT'zhong',N''
    UNION ALL SELECT'zhou',N''
    UNION ALL SELECT'zhu',N''
    UNION ALL SELECT'zhua',N''
    UNION ALL SELECT'zhuai',N''
    UNION ALL SELECT'zhuan',N''
    UNION ALL SELECT'zhuang',N''
    UNION ALL SELECT'zhui',N''
    UNION ALL SELECT'zhun',N''
    UNION ALL SELECT'zhuo',N''
    UNION ALL SELECT'zi',N''--漬唨
    UNION ALL SELECT'zong',N''
    UNION ALL SELECT'zou',N''
    UNION ALL SELECT'zu',N''
    UNION ALL SELECT'zuan',N''
    UNION ALL SELECT'zui',N''
    UNION ALL SELECT'zun',N''
    UNION ALL SELECT'zuo',N'') t  
    WHERE  word >= @word COLLATE  Chinese_PRC_CS_AS_KS_WS  
    ORDER BY  word COLLATE  Chinese_PRC_CS_AS_KS_WS ASC)ELSE  @word END ) 
    SET @i = @i + 1 
    END  
    RETURN  @pinyin   

END   

GO
SELECT dbo.fn_GetPinyin('阿门')

 运行结果如下:

sql server 中的汉字转化为拼音