欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MSSQL汉字转拼音函数实现语句

程序员文章站 2023-11-24 10:54:10
复制代码 代码如下:/* ------------------------------------------------------------- 函数: fn_getp...
复制代码 代码如下:

/* -------------------------------------------------------------
函数: fn_getpinyin
描述: 汉字转拼音(无数据表版)
使用: dbo.fn_getpinyin('*') = zhonghuarenmingongheguo
作者: 流香羽 (改编:tony)
------------------------------------------------------------- */

if object_id('[fn_getpinyin]') is not null
drop function [fn_getpinyin]
go
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 asc) else @word end)
set @i = @i + 1
end
return @pinyin
end
go

select dbo.fn_getpinyin('*')

/*

-------------------------------------------
zuo zhong hua ren min gong he guo

(1 行受影响)
*/