当前位置:   article > 正文

sql函数:汉字转换为拼音

mssql汉字转拼音函数

sql数据库
自定义一个函数
把下面代码写进去

功能是得到汉字拼音首字母

 如下:

  1. create function fun_getPY(@str nvarchar(4000))
  2. returns nvarchar(4000)
  3. as
  4. begin
  5. declare @word nchar(1),@PY nvarchar(4000)
  6. set @PY=''
  7. while len(@str)>0
  8. begin
  9. set @word=left(@str,1)
  10. --如果非汉字字符,返回原字符
  11. set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
  12. then (select top 1 PY from (
  13. select 'A' as PY,N'驁' as word
  14. union all select 'B',N'簿'
  15. union all select 'C',N'錯'
  16. union all select 'D',N'鵽'
  17. union all select 'E',N'樲'
  18. union all select 'F',N'鰒'
  19. union all select 'G',N'腂'
  20. union all select 'H',N'夻'
  21. union all select 'J',N'攈'
  22. union all select 'K',N'穒'
  23. union all select 'L',N'鱳'
  24. union all select 'M',N'旀'
  25. union all select 'N',N'桛'
  26. union all select 'O',N'漚'
  27. union all select 'P',N'曝'
  28. union all select 'Q',N'囕'
  29. union all select 'R',N'鶸'
  30. union all select 'S',N'蜶'
  31. union all select 'T',N'籜'
  32. union all select 'W',N'鶩'
  33. union all select 'X',N'鑂'
  34. union all select 'Y',N'韻'
  35. union all select 'Z',N'咗'
  36. ) T
  37. where word>=@word collate Chinese_PRC_CS_AS_KS_WS
  38. order by PY ASC) else @word end)
  39. set @str=right(@str,len(@str)-1)
  40. end
  41. return @PY
  42. end

--函数调用实例:
select dbo.fun_getPY('中华人民共和国')
结果都为:ZHRMGHG

  1. /* -------------------------------------------------------------
  2. 函数: fn_GetPinyin
  3. 描述: 汉字转拼音(无数据表版)
  4. 使用: dbo.fn_GetPinyin('中华人民共和国') = zhonghuarenmingongheguo
  5. 作者: 流香羽 (改编:Tony)
  6. 博客: http://hi.baidu.com/流香羽
  7. ------------------------------------------------------------- */
  8. IF OBJECT_ID('[fn_GetPinyin]') IS NOT NULL
  9. DROP FUNCTION [fn_GetPinyin]
  10. GO
  11. create function [dbo].[fn_GetPinyin](@words nvarchar(2000))
  12. returns varchar(8000)
  13. as
  14. begin
  15. declare @word nchar(1)
  16. declare @pinyin varchar(8000)
  17. declare @i int
  18. declare @words_len int
  19. declare @unicode int
  20. set @i = 1
  21. set @words = ltrim(rtrim(@words))
  22. set @words_len = len(@words)
  23. while (@i <= @words_len) --循环取字符
  24. begin
  25. set @word = substring(@words, @i, 1)
  26. set @unicode = unicode(@word)
  27. set @pinyin = ISNULL(@pinyin +SPACE(1),'')+
  28. (case when unicode(@word) between 19968 and 19968+20901 then
  29. (select top 1 py from (
  30. select 'a' as py,N'厑' as word
  31. union all select 'ai',N'靉'
  32. union all select 'an',N'黯'
  33. union all select 'ang',N'醠'
  34. union all select 'ao',N'驁'
  35. union all select 'ba',N'欛'
  36. union all select 'bai',N'瓸' --韛兡瓸
  37. union all select 'ban',N'瓣'
  38. union all select 'bang',N'鎊'
  39. union all select 'bao',N'鑤'
  40. union all select 'bei',N'鐾'
  41. union all select 'ben',N'輽'
  42. union all select 'beng',N'鏰'
  43. union all select 'bi',N'鼊'
  44. union all select 'bian',N'變'
  45. union all select 'biao',N'鰾'
  46. union all select 'bie',N'彆'
  47. union all select 'bin',N'鬢'
  48. union all select 'bing',N'靐'
  49. union all select 'bo',N'蔔'
  50. union all select 'bu',N'簿'
  51. union all select 'ca',N'囃'
  52. union all select 'cai',N'乲' --縩乲
  53. union all select 'can',N'爘'
  54. union all select 'cang',N'賶'
  55. union all select 'cao',N'鼜'
  56. union all select 'ce',N'簎'
  57. union all select 'cen',N'笒'
  58. union all select 'ceng',N'乽' --硛硳岾猠乽
  59. union all select 'cha',N'詫'
  60. union all select 'chai',N'囆'
  61. union all select 'chan',N'顫'
  62. union all select 'chang',N'韔'
  63. union all select 'chao',N'觘'
  64. union all select 'che',N'爡'
  65. union all select 'chen',N'讖'
  66. union all select 'cheng',N'秤'
  67. union all select 'chi',N'鷘'
  68. union all select 'chong',N'銃'
  69. union all select 'chou',N'殠'
  70. union all select 'chu',N'矗'
  71. union all select 'chuai',N'踹'
  72. union all select 'chuan',N'鶨'
  73. union all select 'chuang',N'愴'
  74. union all select 'chui',N'顀'
  75. union all select 'chun',N'蠢'
  76. union all select 'chuo',N'縒'
  77. union all select 'ci',N'嗭' --賜嗭
  78. union all select 'cong',N'謥'
  79. union all select 'cou',N'輳'
  80. union all select 'cu',N'顣'
  81. union all select 'cuan',N'爨'
  82. union all select 'cui',N'臎'
  83. union all select 'cun',N'籿'
  84. union all select 'cuo',N'錯'
  85. union all select 'da',N'橽'
  86. union all select 'dai',N'靆'
  87. union all select 'dan',N'饏'
  88. union all select 'dang',N'闣'
  89. union all select 'dao',N'纛'
  90. union all select 'de',N'的'
  91. union all select 'den',N'扽'
  92. union all select 'deng',N'鐙'
  93. union all select 'di',N'螮'
  94. union all select 'dia',N'嗲'
  95. union all select 'dian',N'驔'
  96. union all select 'diao',N'鑃'
  97. union all select 'die',N'嚸' --眰嚸
  98. union all select 'ding',N'顁'
  99. union all select 'diu',N'銩'
  100. union all select 'dong',N'霘'
  101. union all select 'dou',N'鬭'
  102. union all select 'du',N'蠹'
  103. union all select 'duan',N'叾' --籪叾
  104. union all select 'dui',N'譵'
  105. union all select 'dun',N'踲'
  106. union all select 'duo',N'鵽'
  107. union all select 'e',N'鱷'
  108. union all select 'en',N'摁'
  109. union all select 'eng',N'鞥'
  110. union all select 'er',N'樲'
  111. union all select 'fa',N'髮'
  112. union all select 'fan',N'瀪'
  113. union all select 'fang',N'放'
  114. union all select 'fei',N'靅'
  115. union all select 'fen',N'鱝'
  116. union all select 'feng',N'覅'
  117. union all select 'fo',N'梻'
  118. union all select 'fou',N'鴀'
  119. union all select 'fu',N'猤' --鰒猤
  120. union all select 'ga',N'魀'
  121. union all select 'gai',N'瓂'
  122. union all select 'gan',N'灨'
  123. union all select 'gang',N'戇'
  124. union all select 'gao',N'鋯'
  125. union all select 'ge',N'獦'
  126. union all select 'gei',N'給'
  127. union all select 'gen',N'搄'
  128. union all select 'geng',N'堩' --亙堩啹喼嗰
  129. union all select 'gong',N'兣' --熕贑兝兣
  130. union all select 'gou',N'購'
  131. union all select 'gu',N'顧'
  132. union all select 'gua',N'詿'
  133. union all select 'guai',N'恠'
  134. union all select 'guan',N'鱹'
  135. union all select 'guang',N'撗'
  136. union all select 'gui',N'鱥'
  137. union all select 'gun',N'謴'
  138. union all select 'guo',N'腂'
  139. union all select 'ha',N'哈'
  140. union all select 'hai',N'饚'
  141. union all select 'han',N'鶾'
  142. union all select 'hang',N'沆'
  143. union all select 'hao',N'兞'
  144. union all select 'he',N'靏'
  145. union all select 'hei',N'嬒'
  146. union all select 'hen',N'恨'
  147. union all select 'heng',N'堼' --堼囍
  148. union all select 'hong',N'鬨'
  149. union all select 'hou',N'鱟'
  150. union all select 'hu',N'鸌'
  151. union all select 'hua',N'蘳'
  152. union all select 'huai',N'蘾'
  153. union all select 'huan',N'鰀'
  154. union all select 'huang',N'鎤'
  155. union all select 'hui',N'顪'
  156. union all select 'hun',N'諢'
  157. union all select 'huo',N'夻'
  158. union all select 'ji',N'驥'
  159. union all select 'jia',N'嗧'
  160. union all select 'jian',N'鑳'
  161. union all select 'jiang',N'謽'
  162. union all select 'jiao',N'釂'
  163. union all select 'jie',N'繲'
  164. union all select 'jin',N'齽'
  165. union all select 'jing',N'竸'
  166. union all select 'jiong',N'蘔'
  167. union all select 'jiu',N'欍'
  168. union all select 'ju',N'爠'
  169. union all select 'juan',N'羂'
  170. union all select 'jue',N'钁'
  171. union all select 'jun',N'攈'
  172. union all select 'ka',N'鉲'
  173. union all select 'kai',N'乫' --鎎乫
  174. union all select 'kan',N'矙'
  175. union all select 'kang',N'閌'
  176. union all select 'kao',N'鯌'
  177. union all select 'ke',N'騍'
  178. union all select 'ken',N'褃'
  179. union all select 'keng',N'鏗' --巪乬唟厼怾
  180. union all select 'kong',N'廤'
  181. union all select 'kou',N'鷇'
  182. union all select 'ku',N'嚳'
  183. union all select 'kua',N'骻'
  184. union all select 'kuai',N'鱠'
  185. union all select 'kuan',N'窾'
  186. union all select 'kuang',N'鑛'
  187. union all select 'kui',N'鑎'
  188. union all select 'kun',N'睏'
  189. union all select 'kuo',N'穒'
  190. union all select 'la',N'鞡'
  191. union all select 'lai',N'籟'
  192. union all select 'lan',N'糷'
  193. union all select 'lang',N'唥'
  194. union all select 'lao',N'軂'
  195. union all select 'le',N'餎'
  196. union all select 'lei',N'脷' --嘞脷
  197. union all select 'leng',N'睖'
  198. union all select 'li',N'瓈'
  199. union all select 'lia',N'倆'
  200. union all select 'lian',N'纞'
  201. union all select 'liang',N'鍄'
  202. union all select 'liao',N'瞭'
  203. union all select 'lie',N'鱲'
  204. union all select 'lin',N'轥' --轥拎
  205. union all select 'ling',N'炩'
  206. union all select 'liu',N'咯' --瓼甅囖咯
  207. union all select 'long',N'贚'
  208. union all select 'lou',N'鏤'
  209. union all select 'lu',N'氇'
  210. union all select 'lv',N'鑢'
  211. union all select 'luan',N'亂'
  212. union all select 'lue',N'擽'
  213. union all select 'lun',N'論'
  214. union all select 'luo',N'鱳'
  215. union all select 'ma',N'嘛'
  216. union all select 'mai',N'霢'
  217. union all select 'man',N'蘰'
  218. union all select 'mang',N'蠎'
  219. union all select 'mao',N'唜'
  220. union all select 'me',N'癦' --癦呅
  221. union all select 'mei',N'嚜'
  222. union all select 'men',N'們'
  223. union all select 'meng',N'霥' --霿踎
  224. union all select 'mi',N'羃'
  225. union all select 'mian',N'麵'
  226. union all select 'miao',N'廟'
  227. union all select 'mie',N'鱴' --鱴瓱
  228. union all select 'min',N'鰵'
  229. union all select 'ming',N'詺'
  230. union all select 'miu',N'謬'
  231. union all select 'mo',N'耱' --耱乮
  232. union all select 'mou',N'麰' --麰蟱
  233. union all select 'mu',N'旀'
  234. union all select 'na',N'魶'
  235. union all select 'nai',N'錼'
  236. union all select 'nan',N'婻'
  237. union all select 'nang',N'齉'
  238. union all select 'nao',N'臑'
  239. union all select 'ne',N'呢'
  240. union all select 'nei',N'焾' --嫩焾
  241. union all select 'nen',N'嫩'
  242. union all select 'neng',N'能' --莻嗯鈪銰啱
  243. union all select 'ni',N'嬺'
  244. union all select 'nian',N'艌'
  245. union all select 'niang',N'釀'
  246. union all select 'niao',N'脲'
  247. union all select 'nie',N'钀'
  248. union all select 'nin',N'拰'
  249. union all select 'ning',N'濘'
  250. union all select 'niu',N'靵'
  251. union all select 'nong',N'齈'
  252. union all select 'nou',N'譳'
  253. union all select 'nu',N'搙'
  254. union all select 'nv',N'衄'
  255. union all select 'nue',N'瘧'
  256. union all select 'nuan',N'燶' --硸黁燶郍
  257. union all select 'nuo',N'桛'
  258. union all select 'o',N'鞰' --毮夞乯鞰
  259. union all select 'ou',N'漚'
  260. union all select 'pa',N'袙'
  261. union all select 'pai',N'磗' --鎃磗
  262. union all select 'pan',N'鑻'
  263. union all select 'pang',N'胖'
  264. union all select 'pao',N'礮'
  265. union all select 'pei',N'轡'
  266. union all select 'pen',N'喯'
  267. union all select 'peng',N'喸' --浌巼闏乶喸
  268. union all select 'pi',N'鸊'
  269. union all select 'pian',N'騙'
  270. union all select 'piao',N'慓'
  271. union all select 'pie',N'嫳'
  272. union all select 'pin',N'聘'
  273. union all select 'ping',N'蘋'
  274. union all select 'po',N'魄'
  275. union all select 'pou',N'哛' --兺哛
  276. union all select 'pu',N'曝'
  277. union all select 'qi',N'蟿'
  278. union all select 'qia',N'髂'
  279. union all select 'qian',N'縴'
  280. union all select 'qiang',N'瓩' --羻兛瓩
  281. union all select 'qiao',N'躈'
  282. union all select 'qie',N'籡'
  283. union all select 'qin',N'藽'
  284. union all select 'qing',N'櫦'
  285. union all select 'qiong',N'瓗'
  286. union all select 'qiu',N'糗'
  287. union all select 'qu',N'覻'
  288. union all select 'quan',N'勸'
  289. union all select 'que',N'礭'
  290. union all select 'qun',N'囕'
  291. union all select 'ran',N'橪'
  292. union all select 'rang',N'讓'
  293. union all select 'rao',N'繞'
  294. union all select 're',N'熱'
  295. union all select 'ren',N'餁'
  296. union all select 'reng',N'陾'
  297. union all select 'ri',N'馹'
  298. union all select 'rong',N'穃'
  299. union all select 'rou',N'嶿'
  300. union all select 'ru',N'擩'
  301. union all select 'ruan',N'礝'
  302. union all select 'rui',N'壡'
  303. union all select 'run',N'橍' --橍挼
  304. union all select 'ruo',N'鶸'
  305. union all select 'sa',N'栍' --櫒栍
  306. union all select 'sai',N'虄' --簺虄
  307. union all select 'san',N'閐'
  308. union all select 'sang',N'喪'
  309. union all select 'sao',N'髞'
  310. union all select 'se',N'飋' --裇聓
  311. union all select 'sen',N'篸'
  312. union all select 'seng',N'縇' --閪縇
  313. union all select 'sha',N'霎'
  314. union all select 'shai',N'曬'
  315. union all select 'shan',N'鱔'
  316. union all select 'shang',N'緔'
  317. union all select 'shao',N'潲'
  318. union all select 'she',N'欇'
  319. union all select 'shen',N'瘮'
  320. union all select 'sheng',N'賸'
  321. union all select 'shi',N'瓧' --鰘齛兙瓧
  322. union all select 'shou',N'鏉'
  323. union all select 'shu',N'虪'
  324. union all select 'shua',N'誜'
  325. union all select 'shuai',N'卛'
  326. union all select 'shuan',N'腨'
  327. union all select 'shuang',N'灀'
  328. union all select 'shui',N'睡'
  329. union all select 'shun',N'鬊'
  330. union all select 'shuo',N'鑠'
  331. union all select 'si',N'乺' --瀃螦乺
  332. union all select 'song',N'鎹'
  333. union all select 'sou',N'瘶'
  334. union all select 'su',N'鷫'
  335. union all select 'suan',N'算'
  336. union all select 'sui',N'鐩'
  337. union all select 'sun',N'潠'
  338. union all select 'suo',N'蜶'
  339. union all select 'ta',N'襨' --躢襨
  340. union all select 'tai',N'燤'
  341. union all select 'tan',N'賧'
  342. union all select 'tang',N'燙'
  343. union all select 'tao',N'畓' --討畓
  344. union all select 'te',N'蟘'
  345. union all select 'teng',N'朰' --霯唞朰
  346. union all select 'ti',N'趯'
  347. union all select 'tian',N'舚'
  348. union all select 'tiao',N'糶'
  349. union all select 'tie',N'餮'
  350. union all select 'ting',N'乭' --濎乭
  351. union all select 'tong',N'憅'
  352. union all select 'tou',N'透'
  353. union all select 'tu',N'鵵'
  354. union all select 'tuan',N'褖'
  355. union all select 'tui',N'駾'
  356. union all select 'tun',N'坉'
  357. union all select 'tuo',N'籜'
  358. union all select 'wa',N'韤'
  359. union all select 'wai',N'顡'
  360. union all select 'wan',N'贎'
  361. union all select 'wang',N'朢'
  362. union all select 'wei',N'躛'
  363. union all select 'wen',N'璺'
  364. union all select 'weng',N'齆'
  365. union all select 'wo',N'齷'
  366. union all select 'wu',N'鶩'
  367. union all select 'xi',N'衋'
  368. union all select 'xia',N'鏬'
  369. union all select 'xian',N'鼸'
  370. union all select 'xiang',N'鱌'
  371. union all select 'xiao',N'斆'
  372. union all select 'xie',N'躞'
  373. union all select 'xin',N'釁'
  374. union all select 'xing',N'臖'
  375. union all select 'xiong',N'敻'
  376. union all select 'xiu',N'齅'
  377. union all select 'xu',N'蓿'
  378. union all select 'xuan',N'贙'
  379. union all select 'xue',N'瀥'
  380. union all select 'xun',N'鑂'
  381. union all select 'ya',N'齾'
  382. union all select 'yan',N'灩'
  383. union all select 'yang',N'樣'
  384. union all select 'yao',N'鑰'
  385. union all select 'ye',N'岃' --鸈膶岃
  386. union all select 'yi',N'齸'
  387. union all select 'yin',N'檼'
  388. union all select 'ying',N'譍'
  389. union all select 'yo',N'喲'
  390. union all select 'yong',N'醟'
  391. union all select 'you',N'鼬'
  392. union all select 'yu',N'爩'
  393. union all select 'yuan',N'願'
  394. union all select 'yue',N'鸙'
  395. union all select 'yun',N'韻'
  396. union all select 'za',N'雥'
  397. union all select 'zai',N'縡'
  398. union all select 'zan',N'饡'
  399. union all select 'zang',N'臟'
  400. union all select 'zao',N'竈'
  401. union all select 'ze',N'稄'
  402. union all select 'zei',N'鱡'
  403. union all select 'zen',N'囎'
  404. union all select 'zeng',N'贈'
  405. union all select 'zha',N'醡'
  406. union all select 'zhai',N'瘵'
  407. union all select 'zhan',N'驏'
  408. union all select 'zhang',N'瞕'
  409. union all select 'zhao',N'羄'
  410. union all select 'zhe',N'鷓'
  411. union all select 'zhen',N'黮'
  412. union all select 'zheng',N'證'
  413. union all select 'zhi',N'豒'
  414. union all select 'zhong',N'諥'
  415. union all select 'zhou',N'驟'
  416. union all select 'zhu',N'鑄'
  417. union all select 'zhua',N'爪'
  418. union all select 'zhuai',N'跩'
  419. union all select 'zhuan',N'籑'
  420. union all select 'zhuang',N'戅'
  421. union all select 'zhui',N'鑆'
  422. union all select 'zhun',N'稕'
  423. union all select 'zhuo',N'籱'
  424. union all select 'zi',N'漬' --漬唨
  425. union all select 'zong',N'縱'
  426. union all select 'zou',N'媰'
  427. union all select 'zu',N'謯'
  428. union all select 'zuan',N'攥'
  429. union all select 'zui',N'欈'
  430. union all select 'zun',N'銌'
  431. union all select 'zuo',N'咗') t
  432. where word >= @word collate Chinese_PRC_CS_AS_KS_WS
  433. order by word collate Chinese_PRC_CS_AS_KS_WS ASC) else @word end)
  434. set @i = @i + 1
  435. end
  436. return @pinyin
  437. END
  438. GO
  439. SELECT dbo.fn_GetPinyin('中华人民共和国')
  440. /*
  441. -------------------------------------------
  442. zuo zhong hua ren min gong he guo
  443. (1 行受影响)
  444. */

转载于:https://www.cnblogs.com/ret00100/archive/2010/08/06/1793725.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/203915
推荐阅读
相关标签
  

闽ICP备14008679号