sql自动生成汉语拼音和首字母函数

1.Sql server自动生成拼音的函数

/*
 根据汉字获取全拼
 1.生成所有读音临时表
 2.根据Chinese_PRC_CS_AS_KS_WS 排序获取读音
*/
CREATE function f_GetPying(@str varchar(100))
returns varchar(8000)
as
begin
 declare @re varchar(8000)
 --生成临时表
 declare @t table(chr nchar(1) collate  Chinese_PRC_CS_AS_KS_WS,py nvarchar(20)) 
 insert into @t select'','a' 
 insert into @t select'','aes' 
 insert into @t select'','ai' 
 insert into @t select'','an' 
 insert into @t select'','ang' 
 insert into @t select'','ao' 
 insert into @t select'','ba' 
 insert into @t select'','bai' 
 insert into @t select'','baike' 
 insert into @t select'','baiwa' 
 insert into @t select'','ban' 
 insert into @t select'','bang' 
 insert into @t select'','bao' 
 insert into @t select'','be' 
 insert into @t select'','bei' 
 insert into @t select'','ben' 
 insert into @t select'','beng' 
 insert into @t select'','bi' 
 insert into @t select'','bian' 
 insert into @t select'','uu' 
 insert into @t select'','biao' 
 insert into @t select'','bie' 
 insert into @t select'','bin' 
 insert into @t select'','bing' 
 insert into @t select'','bo' 
 insert into @t select'','bu' 
 insert into @t select'','ca' 
 insert into @t select'','cai' 
 insert into @t select'','cal' 
 insert into @t select'','can' 
 insert into @t select'','cang' 
 insert into @t select'','cao' 
 insert into @t select'','ce' 
 insert into @t select'','cen' 
 insert into @t select'','ceng' 
 insert into @t select'','ceok' 
 insert into @t select'','ceom' 
 insert into @t select'','ceon' 
 insert into @t select'','ceor' 
 insert into @t select'','cha' 
 insert into @t select'','chai' 
 insert into @t select'辿','chan' 
 insert into @t select'','chang' 
 insert into @t select'','chao' 
 insert into @t select'','che' 
 insert into @t select'','chen' 
 insert into @t select'','cheng' 
 insert into @t select'','chi' 
 insert into @t select'','chong' 
 insert into @t select'','chou' 
 insert into @t select'','chu' 
 insert into @t select'','chuai' 
 insert into @t select'','chuan' 
 insert into @t select'','chuang' 
 insert into @t select'','chui' 
 insert into @t select'','chun' 
 insert into @t select'','chuo' 
 insert into @t select'','ci' 
 insert into @t select'','cis' 
 insert into @t select'','cong' 
 insert into @t select'','cou' 
 insert into @t select'','cu' 
 insert into @t select'','cuan' 
 insert into @t select'','cui' 
 insert into @t select'','cun' 
 insert into @t select'','cuo' 
 insert into @t select'','chua' 
 insert into @t select'','da' 
 insert into @t select'','dai' 
 insert into @t select'','dan' 
 insert into @t select'','dang' 
 insert into @t select'','dao' 
 insert into @t select'','de' 
 insert into @t select'','dem' 
 insert into @t select'','den' 
 insert into @t select'','deng' 
 insert into @t select'','di' 
 insert into @t select'','dia' 
 insert into @t select'','dian' 
 insert into @t select'','diao' 
 insert into @t select'','die' 
 insert into @t select'','dei' 
 insert into @t select'','dim' 
 insert into @t select'','ding' 
 insert into @t select'','diu' 
 insert into @t select'','dong' 
 insert into @t select'','dou' 
 insert into @t select'','du' 
 insert into @t select'','duan' 
 insert into @t select'','dug' 
 insert into @t select'','dui' 
 insert into @t select'','dun' 
 insert into @t select'','duo' 
 insert into @t select'','e' 
 insert into @t select'','en' 
 insert into @t select'','eng' 
 insert into @t select'','eo' 
 insert into @t select'','eol' 
 insert into @t select'','eos' 
 insert into @t select'','er' 
 insert into @t select'','fa' 
 insert into @t select'','fan' 
 insert into @t select'','fang' 
 insert into @t select'','fei' 
 insert into @t select'','fen' 
 insert into @t select'','feng' 
 insert into @t select'','fenwa' 
 insert into @t select'','fiao' 
 insert into @t select'','fo' 
 insert into @t select'','fou' 
 insert into @t select'','fu' 
 insert into @t select'','fui' 
 insert into @t select'','ga' 
 insert into @t select'','gai' 
 insert into @t select'','gan' 
 insert into @t select'','gang' 
 insert into @t select'','gao' 
 insert into @t select'','ge' 
 insert into @t select'','gei' 
 insert into @t select'','gen' 
 insert into @t select'','geng' 
 insert into @t select'','geu' 
 insert into @t select'','gib' 
 insert into @t select'','go' 
 insert into @t select'','gong' 
 insert into @t select'','gongfen' 
 insert into @t select'','gongli' 
 insert into @t select'','gou' 
 insert into @t select'','gu' 
 insert into @t select'','gua' 
 insert into @t select'','guai' 
 insert into @t select'','guan' 
 insert into @t select'','guang' 
 insert into @t select'','gui' 
 insert into @t select'','gun' 
 insert into @t select'','guo' 
 insert into @t select'','ha' 
 insert into @t select'','hai' 
 insert into @t select'','hal' 
 insert into @t select'','han' 
 insert into @t select'','hang' 
 insert into @t select'','hao' 
 insert into @t select'','haoke' 
 insert into @t select'','he' 
 insert into @t select'','hei' 
 insert into @t select'','hen' 
 insert into @t select'','heng' 
 insert into @t select'','heui' 
 insert into @t select'','ho' 
 insert into @t select'','hol' 
 insert into @t select'','hong' 
 insert into @t select'','hou' 
 insert into @t select'','hu' 
 insert into @t select'','hua' 
 insert into @t select'','huai' 
 insert into @t select'','huan' 
 insert into @t select'','huang' 
 insert into @t select'','hui' 
 insert into @t select'','hun' 
 insert into @t select'','huo' 
 insert into @t select'','geo' 
 insert into @t select'','hwa' 
 insert into @t select'','ji' 
 insert into @t select'','jia' 
 insert into @t select'','jialun' 
 insert into @t select'','jian' 
 insert into @t select'','jiang' 
 insert into @t select'','jiao' 
 insert into @t select'','jie' 
 insert into @t select'','jin' 
 insert into @t select'','jing' 
 insert into @t select'','jiong' 
 insert into @t select'','jiu' 
 insert into @t select'','jou' 
 insert into @t select'','ju' 
 insert into @t select'','juan' 
 insert into @t select'','jue' 
 insert into @t select'','jun' 
 insert into @t select'','ka' 
 insert into @t select'','kai' 
 insert into @t select'','kal' 
 insert into @t select'','kan' 
 insert into @t select'','hem' 
 insert into @t select'','kang' 
 insert into @t select'','kao' 
 insert into @t select'','ke' 
 insert into @t select'','ken' 
 insert into @t select'','keng' 
 insert into @t select'','keo' 
 insert into @t select'','keol' 
 insert into @t select'','keos' 
 insert into @t select'','keum' 
 insert into @t select'','ki' 
 insert into @t select'','kong' 
 insert into @t select'','kos' 
 insert into @t select'','kou' 
 insert into @t select'','ku' 
 insert into @t select'','kua' 
 insert into @t select'','kuai' 
 insert into @t select'','kuan' 
 insert into @t select'','kuang' 
 insert into @t select'','kui' 
 insert into @t select'','kun' 
 insert into @t select'','kuo' 
 insert into @t select'','kweok' 
 insert into @t select'','la' 
 insert into @t select'','lai' 
 insert into @t select'','lan' 
 insert into @t select'','lang' 
 insert into @t select'','lao' 
 insert into @t select'','le' 
 insert into @t select'','lei' 
 insert into @t select'','leng' 
 insert into @t select'','li' 
 insert into @t select'','lia' 
 insert into @t select'','lian' 
 insert into @t select'','liang' 
 insert into @t select'','liao' 
 insert into @t select'','lie' 
 insert into @t select'','lin' 
 insert into @t select'','ling' 
 insert into @t select'','liu' 
 insert into @t select'','liwa' 
 insert into @t select'','lo' 
 insert into @t select'','long' 
 insert into @t select'','lou' 
 insert into @t select'','lu' 
 insert into @t select'','lv' 
 insert into @t select'','lue' 
 insert into @t select'','luan' 
 insert into @t select'','lun' 
 insert into @t select'','luo' 
 insert into @t select'','m' 
 insert into @t select'','ma' 
 insert into @t select'','hweong' 
 insert into @t select'','mai' 
 insert into @t select'','man' 
 insert into @t select'','mang' 
 insert into @t select'','mangmi' 
 insert into @t select'','mao' 
 insert into @t select'','mas' 
 insert into @t select'','me' 
 insert into @t select'','mei' 
 insert into @t select'','men' 
 insert into @t select'','meng' 
 insert into @t select'','meo' 
 insert into @t select'','mi' 
 insert into @t select'','mian' 
 insert into @t select'','miao' 
 insert into @t select'','mie' 
 insert into @t select'','miliklanm' 
 insert into @t select'','min' 
 insert into @t select'','lem' 
 insert into @t select'','ming' 
 insert into @t select'','miu' 
 insert into @t select'','mo' 
 insert into @t select'','mol' 
 insert into @t select'','mou' 
 insert into @t select'','mu' 
 insert into @t select'','myeo' 
 insert into @t select'','myeon' 
 insert into @t select'','myeong' 
 insert into @t select'','na' 
 insert into @t select'','nai' 
 insert into @t select'','nan' 
 insert into @t select'','nang' 
 insert into @t select'','keg' 
 insert into @t select'','nao' 
 insert into @t select'','ne' 
 insert into @t select'','nei' 
 insert into @t select'','nem' 
 insert into @t select'','nen' 
 insert into @t select'','neus' 
 insert into @t select'','ngag' 
 insert into @t select'','ngai' 
 insert into @t select'','ngam' 
 insert into @t select'','ni' 
 insert into @t select'','nian' 
 insert into @t select'','niang' 
 insert into @t select'','niao' 
 insert into @t select'','nie' 
 insert into @t select'','nin' 
 insert into @t select'','ning' 
 insert into @t select'','niu' 
 insert into @t select'','nong' 
 insert into @t select'','nou' 
 insert into @t select'','nu' 
 insert into @t select'','nv' 
 insert into @t select'','nue' 
 insert into @t select'','nve' 
 insert into @t select'','nuan' 
 insert into @t select'','nun' 
 insert into @t select'','nung' 
 insert into @t select'','nuo' 
 insert into @t select'','o' 
 insert into @t select'','oes' 
 insert into @t select'','ol' 
 insert into @t select'','on' 
 insert into @t select'','ou' 
 insert into @t select'','pa' 
 insert into @t select'','pai' 
 insert into @t select'','pak' 
 insert into @t select'','pan' 
 insert into @t select'','pang' 
 insert into @t select'','pao' 
 insert into @t select'','pei' 
 insert into @t select'','pen' 
 insert into @t select'','peng' 
 insert into @t select'','peol' 
 insert into @t select'','phas' 
 insert into @t select'','phdeng' 
 insert into @t select'','phoi' 
 insert into @t select'','phos' 
 insert into @t select'','pi' 
 insert into @t select'','pian' 
 insert into @t select'','piao' 
 insert into @t select'','pie' 
 insert into @t select'丿','pianpang' 
 insert into @t select'','pin' 
 insert into @t select'','ping' 
 insert into @t select'','po' 
 insert into @t select'','pou' 
 insert into @t select'','deo' 
 insert into @t select'','ppun' 
 insert into @t select'','pu' 
 insert into @t select'','qi' 
 insert into @t select'','qia' 
 insert into @t select'','qian' 
 insert into @t select'','qiang' 
 insert into @t select'','qianke' 
 insert into @t select'','qianwa' 
 insert into @t select'','qiao' 
 insert into @t select'','qie' 
 insert into @t select'','qin' 
 insert into @t select'','kem' 
 insert into @t select'','qing' 
 insert into @t select'','qiong' 
 insert into @t select'','qiu' 
 insert into @t select'','qu' 
 insert into @t select'','keop' 
 insert into @t select'','quan' 
 insert into @t select'','que' 
 insert into @t select'','qun' 
 insert into @t select'','ra' 
 insert into @t select'','ram' 
 insert into @t select'','ran' 
 insert into @t select'','rang' 
 insert into @t select'','rao' 
 insert into @t select'','re' 
 insert into @t select'','ren' 
 insert into @t select'','reng' 
 insert into @t select'','ri' 
 insert into @t select'','rong' 
 insert into @t select'','rou' 
 insert into @t select'嶿','ru' 
 insert into @t select'','ruan' 
 insert into @t select'','rui' 
 insert into @t select'','run' 
 insert into @t select'','ruo' 
 insert into @t select'','sa' 
 insert into @t select'','saeng' 
 insert into @t select'','sai' 
 insert into @t select'','sal' 
 insert into @t select'','san' 
 insert into @t select'','sang' 
 insert into @t select'','sao' 
 insert into @t select'','se' 
 insert into @t select'','sed' 
 insert into @t select'','sei' 
 insert into @t select'','sen' 
 insert into @t select'','seng' 
 insert into @t select'','seo' 
 insert into @t select'','seon' 
 insert into @t select'','sha' 
 insert into @t select'','shai' 
 insert into @t select'','shan' 
 insert into @t select'','shang' 
 insert into @t select'','shao' 
 insert into @t select'','she' 
 insert into @t select'','shen' 
 insert into @t select'','sheng' 
 insert into @t select'','shi' 
 insert into @t select'','shike' 
 insert into @t select'','shiwa' 
 insert into @t select'','shou' 
 insert into @t select'','shu' 
 insert into @t select'','shua' 
 insert into @t select'','shuai' 
 insert into @t select'','shuan' 
 insert into @t select'','shuang' 
 insert into @t select'','shei' 
 insert into @t select'','shui' 
 insert into @t select'','shun' 
 insert into @t select'','shuo' 
 insert into @t select'','si' 
 insert into @t select'','so' 
 insert into @t select'','sol' 
 insert into @t select'','song' 
 insert into @t select'','sou' 
 insert into @t select'','su' 
 insert into @t select'','suan' 
 insert into @t select'','sui' 
 insert into @t select'','sun' 
 insert into @t select'','suo' 
 insert into @t select'','ta' 
 insert into @t select'','tae' 
 insert into @t select'','tai' 
 insert into @t select'','tan' 
 insert into @t select'','tang' 
 insert into @t select'','tao' 
 insert into @t select'','tap' 
 insert into @t select'','te' 
 insert into @t select'','teng' 
 insert into @t select'','teo' 
 insert into @t select'','teul' 
 insert into @t select'','ti' 
 insert into @t select'','tian' 
 insert into @t select'','tiao' 
 insert into @t select'','tie' 
 insert into @t select'','ting' 
 insert into @t select'','tol' 
 insert into @t select'','tong' 
 insert into @t select'','tou' 
 insert into @t select'','tu' 
 insert into @t select'','tuan' 
 insert into @t select'','tui' 
 insert into @t select'','tun' 
 insert into @t select'','tuo' 
 insert into @t select'','wa' 
 insert into @t select'','wai' 
 insert into @t select'','wan' 
 insert into @t select'','wang' 
 insert into @t select'','wei' 
 insert into @t select'','wen' 
 insert into @t select'','weng' 
 insert into @t select'','wo' 
 insert into @t select'','wu' 
 insert into @t select'','xi' 
 insert into @t select'','ei' 
 insert into @t select'','xia' 
 insert into @t select'','xian' 
 insert into @t select'','xiang' 
 insert into @t select'','xiao' 
 insert into @t select'','xie' 
 insert into @t select'','xin' 
 insert into @t select'','xing' 
 insert into @t select'','xiong' 
 insert into @t select'','xiu' 
 insert into @t select'','xu' 
 insert into @t select'','xuan' 
 insert into @t select'','xue' 
 insert into @t select'','xun' 
 insert into @t select'','ya' 
 insert into @t select'','yan' 
 insert into @t select'','eom' 
 insert into @t select'','yang' 
 insert into @t select'','yao' 
 insert into @t select'','ye' 
 insert into @t select'','yen' 
 insert into @t select'','yi' 
 insert into @t select'','i' 
 insert into @t select'','yin' 
 insert into @t select'','ying' 
 insert into @t select'','yo' 
 insert into @t select'','yong' 
 insert into @t select'','you' 
 insert into @t select'','yu' 
 insert into @t select'','yuan' 
 insert into @t select'','yue' 
 insert into @t select'','yun' 
 insert into @t select'','za' 
 insert into @t select'','zai' 
 insert into @t select'','zan' 
 insert into @t select'','zang' 
 insert into @t select'','zao' 
 insert into @t select'','ze' 
 insert into @t select'','zei' 
 insert into @t select'','zen' 
 insert into @t select'','zeng' 
 insert into @t select'','zha' 
 insert into @t select'','gad' 
 insert into @t select'','zhai' 
 insert into @t select'','zhan' 
 insert into @t select'','zhang' 
 insert into @t select'','zhao' 
 insert into @t select'','zhe' 
 insert into @t select'','zhen' 
 insert into @t select'','zheng' 
 insert into @t select'','zhi' 
 insert into @t select'','zhong' 
 insert into @t select'','zhou' 
 insert into @t select'','zhu' 
 insert into @t select'','zhua' 
 insert into @t select'','zhuan' 
 insert into @t select'','zhuai' 
 insert into @t select'','zhuang' 
 insert into @t select'','zhui' 
 insert into @t select'','zhun' 
 insert into @t select'','zhuo' 
 insert into @t select'','zi' 
 insert into @t select'','zo' 
 insert into @t select'','zong' 
 insert into @t select'','zou' 
 insert into @t select'','zu' 
 insert into @t select'','zuan' 
 insert into @t select'','zui' 
 insert into @t select'','zun' 
 insert into @t select'','zuo' 
 
 declare @strlen int 
 select @strlen=len(@str),@re=''
 while @strlen>0
 begin     
      select top 1 @re=UPPER(substring(py,1,1) )+substring(py,2,len(py))+@re,@strlen=@strlen-1 
      from @t a where chr<=substring(@str,@strlen,1) 
      order by chr collate Chinese_PRC_CS_AS_KS_WS  desc 
      if @@rowcount=0
        select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
   end
 return(@re)
end

测试:

SELECT dbo.fn_GetPying('中文字段') AS pymc

2.sql server自动生成拼音首字母的函数

--生成拼音首码  
  CREATE  function  fn_GetPy(@str  nvarchar(4000))  
  returns  nvarchar(4000)  
  --WITH  ENCRYPTION  
  as  
  begin  
  declare  @intLen int  
  declare  @strRet nvarchar(4000)  
  declare  @temp  nvarchar(100)  
  set  @intLen  =  len(@str)  
  set  @strRet  =  ''  
  while  @intLen  >  0  
  begin  
  set  @temp  =  ''  
  select  @temp  =  case    
  when  substring(@str,@intLen,1)  >=  ''  then  'Z'  
  when  substring(@str,@intLen,1)  >=  ''  then  'Y'  
  when  substring(@str,@intLen,1)  >=  ''  then  'X'  
  when  substring(@str,@intLen,1)  >=  ''  then  'W'  
  when  substring(@str,@intLen,1)  >=  ''  then  'T'  
  when  substring(@str,@intLen,1)  >=  ''  then  'S'  
  when  substring(@str,@intLen,1)  >=  ''  then  'R'  
  when  substring(@str,@intLen,1)  >=  ''  then  'Q'  
  when  substring(@str,@intLen,1)  >=  ''  then  'P'  
  when  substring(@str,@intLen,1)  >=  ''  then  'O'  
  when  substring(@str,@intLen,1)  >=  ''  then  'N'  
  when  substring(@str,@intLen,1)  >=  ''  then  'M'  
  when  substring(@str,@intLen,1)  >=  ''  then  'L'  
  when  substring(@str,@intLen,1)  >=  ''  then  'K'  
  when  substring(@str,@intLen,1)  >=  ''  then    'J'  
  when  substring(@str,@intLen,1)  >=  ''  then  'H'  
  when  substring(@str,@intLen,1)  >=  ''  then  'G'  
  when  substring(@str,@intLen,1)  >=  ''  then  'F'  
  when  substring(@str,@intLen,1)  >=  ''  then  'E'  
  when  substring(@str,@intLen,1)  >=  ''  then  'D'  
  when  substring(@str,@intLen,1)  >=  ''  then  'C'  
  when  substring(@str,@intLen,1)  >=  ''  then  'B'  
  when  substring(@str,@intLen,1)  >=  ''  then  'A'  
  else  rtrim(ltrim(substring(@str,@intLen,1)))  
  end  
  --对于汉字特殊字符,不生成拼音码  
  if  (ascii(@temp)>127)  set  @temp  =  ''  
  --对于英文中小括号,不生成拼音码  
  if  @temp  =  '('  or  @temp  =  ')'  set  @temp  =  ''  
  select  @strRet  =  @temp  +  @strRet  
  set  @intLen  =  @intLen  -  1  
  end  
  return  lower(@strRet)  
  end  

测试:

SELECT dbo.fn_GetPy('中文字段') AS pymc
原文地址:https://www.cnblogs.com/shenyixin/p/5262380.html