首页 > 数据库技术 > 详细

2019-2-14SQLserver中拼音查询数据

时间:2019-02-14 14:54:36      阅读:200      评论:0      收藏:0      [点我收藏+]

SQLserver中获取文字全拼音

技术分享图片
  1 CREATE function [dbo].[f_GetPinyin](@words nvarchar(2000))   
  2 returns varchar(8000)   
  3 as  
  4 begin  
  5 declare @word nchar(1)   
  6 declare @pinyin varchar(8000)   
  7 declare @i int  
  8 declare @words_len int  
  9 declare @unicode int  
 10 set @i = 1   
 11 set @words = ltrim(rtrim(@words))   
 12 set @words_len = len(@words)   
 13 while (@i <= @words_len) --循环取字符   
 14 begin  
 15 set @word = substring(@words, @i, 1)   
 16 set @unicode = unicode(@word)   
 17 set @pinyin = ISNULL(@pinyin,‘‘)+   
 18 (case when unicode(@word) between 19968 and 19968+20901 then  
 19 (select top 1 py from (   
 20 select a as py,N as word   
 21 union all select ai,N  
 22 union all select an,N  
 23 union all select ang,N  
 24 union all select ao,N  
 25 union all select ba,N  
 26 union all select bai,N 
 27 union all select ban,N  
 28 union all select bang,N  
 29 union all select bao,N  
 30 union all select bei,N  
 31 union all select ben,N  
 32 union all select beng,N  
 33 union all select bi,N  
 34 union all select bian,N  
 35 union all select biao,N  
 36 union all select bie,N  
 37 union all select bin,N  
 38 union all select bing,N  
 39 union all select bo,N  
 40 union all select bu,N簿  
 41 union all select ca,N  
 42 union all select cai,N 
 43 union all select can,N  
 44 union all select cang,N  
 45 union all select cao,N  
 46 union all select ce,N  
 47 union all select cen,N  
 48 union all select ceng,N 
 49 union all select cha,N  
 50 union all select chai,N  
 51 union all select chan,N  
 52 union all select chang,N  
 53 union all select chao,N  
 54 union all select che,N  
 55 union all select chen,N  
 56 union all select cheng,N  
 57 union all select chi,N  
 58 union all select chong,N  
 59 union all select chou,N  
 60 union all select chu,N  
 61 union all select chuai,N  
 62 union all select chuan,N  
 63 union all select chuang,N  
 64 union all select chui,N  
 65 union all select chun,N  
 66 union all select chuo,N  
 67 union all select ci,N 
 68 union all select cong,N  
 69 union all select cou,N  
 70 union all select cu,N  
 71 union all select cuan,N  
 72 union all select cui,N  
 73 union all select cun,N籿  
 74 union all select cuo,N  
 75 union all select da,N  
 76 union all select dai,N  
 77 union all select dan,N  
 78 union all select dang,N  
 79 union all select dao,N  
 80 union all select de,N  
 81 union all select den,N  
 82 union all select deng,N  
 83 union all select di,N  
 84 union all select dia,N  
 85 union all select dian,N  
 86 union all select diao,N  
 87 union all select die,N 
 88 union all select ding,N  
 89 union all select diu,N  
 90 union all select dong,N  
 91 union all select dou,N  
 92 union all select du,N  
 93 union all select duan,N 
 94 union all select dui,N  
 95 union all select dun,N  
 96 union all select duo,N  
 97 union all select e,N  
 98 union all select en,N  
 99 union all select eng,N  
100 union all select er,N  
101 union all select fa,N  
102 union all select fan,N  
103 union all select fang,N  
104 union all select fei,N  
105 union all select fen,N  
106 union all select feng,N  
107 union all select fo,N  
108 union all select fou,N  
109 union all select fu,N 
110 union all select ga,N  
111 union all select gai,N  
112 union all select gan,N  
113 union all select gang,N  
114 union all select gao,N  
115 union all select ge,N  
116 union all select gei,N  
117 union all select gen,N  
118 union all select geng,N 
119 union all select gong,N 
120 union all select gou,N  
121 union all select gu,N  
122 union all select gua,N詿  
123 union all select guai,N  
124 union all select guan,N  
125 union all select guang,N  
126 union all select gui,N  
127 union all select gun,N  
128 union all select guo,N  
129 union all select ha,N  
130 union all select hai,N  
131 union all select han,N  
132 union all select hang,N  
133 union all select hao,N  
134 union all select he,N  
135 union all select hei,N  
136 union all select hen,N  
137 union all select heng,N 
138 union all select hong,N  
139 union all select hou,N  
140 union all select hu,N  
141 union all select hua,N  
142 union all select huai,N  
143 union all select huan,N  
144 union all select huang,N  
145 union all select hui,N  
146 union all select hun,N  
147 union all select huo,N  
148 union all select ji,N  
149 union all select jia,N  
150 union all select jian,N  
151 union all select jiang,N  
152 union all select jiao,N  
153 union all select jie,N  
154 union all select jin,N  
155 union all select jing,N  
156 union all select jiong,N  
157 union all select jiu,N  
158 union all select ju,N  
159 union all select juan,N  
160 union all select jue,N  
161 union all select jun,N  
162 union all select ka,N  
163 union all select kai,N 
164 union all select kan,N  
165 union all select kang,N  
166 union all select kao,N  
167 union all select ke,N  
168 union all select ken,N  
169 union all select keng,N 
170 union all select kong,N  
171 union all select kou,N  
172 union all select ku,N  
173 union all select kua,N  
174 union all select kuai,N  
175 union all select kuan,N  
176 union all select kuang,N  
177 union all select kui,N  
178 union all select kun,N  
179 union all select kuo,N  
180 union all select la,N  
181 union all select lai,N  
182 union all select lan,N  
183 union all select lang,N  
184 union all select lao,N  
185 union all select le,N  
186 union all select lei,N 
187 union all select leng,N  
188 union all select li,N  
189 union all select lia,N  
190 union all select lian,N  
191 union all select liang,N  
192 union all select liao,N  
193 union all select lie,N  
194 union all select lin,N 
195 union all select ling,N  
196 union all select liu,N 
197 union all select long,N  
198 union all select lou,N  
199 union all select lu,N  
200 union all select lv,N  
201 union all select luan,N  
202 union all select lue,N  
203 union all select lun,N  
204 union all select luo,N  
205 union all select ma,N  
206 union all select mai,N  
207 union all select man,N  
208 union all select mang,N  
209 union all select mao,N  
210 union all select me,N 
211 union all select mei,N  
212 union all select men,N  
213 union all select meng,N 
214 union all select mi,N  
215 union all select mian,N  
216 union all select miao,N  
217 union all select mie,N 
218 union all select min,N  
219 union all select ming,N  
220 union all select miu,N  
221 union all select mo,N 
222 union all select mou,N 
223 union all select mu,N  
224 union all select na,N  
225 union all select nai,N  
226 union all select nan,N  
227 union all select nang,N  
228 union all select nao,N  
229 union all select ne,N  
230 union all select nei,N 
231 union all select nen,N  
232 union all select neng,N 
233 union all select ni,N  
234 union all select nian,N  
235 union all select niang,N  
236 union all select niao,N  
237 union all select nie,N  
238 union all select nin,N  
239 union all select ning,N  
240 union all select niu,N  
241 union all select nong,N  
242 union all select nou,N  
243 union all select nu,N  
244 union all select nv,N  
245 union all select nue,N  
246 union all select nuan,N 
247 union all select nuo,N  
248 union all select o,N 
249 union all select ou,N  
250 union all select pa,N  
251 union all select pai,N 
252 union all select pan,N  
253 union all select pang,N  
254 union all select pao,N  
255 union all select pei,N  
256 union all select pen,N  
257 union all select peng,N 
258 union all select pi,N  
259 union all select pian,N  
260 union all select piao,N  
261 union all select pie,N  
262 union all select pin,N  
263 union all select ping,N  
264 union all select po,N  
265 union all select pou,N 
266 union all select pu,N  
267 union all select qi,N  
268 union all select qia,N  
269 union all select qian,N  
270 union all select qiang,N 
271 union all select qiao,N  
272 union all select qie,N  
273 union all select qin,N  
274 union all select qing,N  
275 union all select qiong,N  
276 union all select qiu,N  
277 union all select qu,N  
278 union all select quan,N  
279 union all select que,N  
280 union all select qun,N  
281 union all select ran,N  
282 union all select rang,N  
283 union all select rao,N  
284 union all select re,N  
285 union all select ren,N  
286 union all select reng,N  
287 union all select ri,N  
288 union all select rong,N  
289 union all select rou,N嶿  
290 union all select ru,N  
291 union all select ruan,N  
292 union all select rui,N  
293 union all select run,N 
294 union all select ruo,N  
295 union all select sa,N 
296 union all select sai,N 
297 union all select san,N  
298 union all select sang,N  
299 union all select sao,N  
300 union all select se,N 
301 union all select sen,N  
302 union all select seng,N 
303 union all select sha,N  
304 union all select shai,N  
305 union all select shan,N  
306 union all select shang,N  
307 union all select shao,N  
308 union all select she,N  
309 union all select shen,N  
310 union all select sheng,N  
311 union all select shi,N 
312 union all select shou,N  
313 union all select shu,N  
314 union all select shua,N  
315 union all select shuai,N  
316 union all select shuan,N  
317 union all select shuang,N  
318 union all select shui,N  
319 union all select shun,N  
320 union all select shuo,N  
321 union all select si,N 
322 union all select song,N  
323 union all select sou,N  
324 union all select su,N  
325 union all select suan,N  
326 union all select sui,N  
327 union all select sun,N  
328 union all select suo,N  
329 union all select ta,N 
330 union all select tai,N  
331 union all select tan,N  
332 union all select tang,N  
333 union all select tao,N 
334 union all select te,N  
335 union all select teng,N 
336 union all select ti,N  
337 union all select tian,N  
338 union all select tiao,N  
339 union all select tie,N  
340 union all select ting,N 
341 union all select tong,N  
342 union all select tou,N  
343 union all select tu,N  
344 union all select tuan,N  
345 union all select tui,N  
346 union all select tun,N  
347 union all select tuo,N  
348 union all select wa,N  
349 union all select wai,N  
350 union all select wan,N  
351 union all select wang,N  
352 union all select wei,N  
353 union all select wen,N  
354 union all select weng,N  
355 union all select wo,N  
356 union all select wu,N  
357 union all select xi,N  
358 union all select xia,N  
359 union all select xian,N  
360 union all select xiang,N  
361 union all select xiao,N  
362 union all select xie,N  
363 union all select xin,N  
364 union all select xing,N  
365 union all select xiong,N  
366 union all select xiu,N  
367 union all select xu,N  
368 union all select xuan,N  
369 union all select xue,N  
370 union all select xun,N  
371 union all select ya,N  
372 union all select yan,N  
373 union all select yang,N  
374 union all select yao,N  
375 union all select ye,N 
376 union all select yi,N  
377 union all select yin,N  
378 union all select ying,N  
379 union all select yo,N  
380 union all select yong,N  
381 union all select you,N  
382 union all select yu,N  
383 union all select yuan,N  
384 union all select yue,N  
385 union all select yun,N  
386 union all select za,N  
387 union all select zai,N  
388 union all select zan,N  
389 union all select zang,N  
390 union all select zao,N  
391 union all select ze,N  
392 union all select zei,N  
393 union all select zen,N  
394 union all select zeng,N  
395 union all select zha,N  
396 union all select zhai,N  
397 union all select zhan,N  
398 union all select zhang,N  
399 union all select zhao,N  
400 union all select zhe,N  
401 union all select zhen,N  
402 union all select zheng,N  
403 union all select zhi,N  
404 union all select zhong,N  
405 union all select zhou,N  
406 union all select zhu,N  
407 union all select zhua,N  
408 union all select zhuai,N  
409 union all select zhuan,N  
410 union all select zhuang,N  
411 union all select zhui,N  
412 union all select zhun,N  
413 union all select zhuo,N  
414 union all select zi,N 
415 union all select zong,N  
416 union all select zou,N  
417 union all select zu,N  
418 union all select zuan,N  
419 union all select zui,N  
420 union all select zun,N  
421 union all select zuo,N) t   
422 where word >= @word collate Chinese_PRC_CS_AS_KS_WS   
423 order by word ASC) else @word end)   
424 set @i = @i + 1   
425 end  
426 return @pinyin   
427 END  
428    
429 GO
获取中文全拼音的SQL函数

SQLserver中获取单字全拼音

技术分享图片
 1 CREATE function [dbo].[FUN_GETPY](@STR nvarchar(4000)) 
 2 returns nvarchar(4000) 
 3 as
 4 begin
 5  declare @word nchar(1), @PY nvarchar(4000)
 6  
 7  set @PY = ‘‘
 8  while len(@str) > 0 
 9  begin
10   set @word = left(@str, 1) --如果非汉字字符,返回原字符 
11   set @PY = @PY +
12   --简体GBK码汉字UNICODE值从19968开始, WINDOWS多国汉字,UNICODE目前收录汉字共20902个
13   (case when unicode(@word) between 19968 and 19968 + 20901 then   
14    (select top 1 PY from 
15    ( select A as PY, N as word 
16     union all 
17      select B,N簿
18     union all 
19      select C,N
20     union all 
21      select D,N
22     union all 
23      select E,N
24     union all 
25      select F,N
26     union all 
27      select G,N
28     union all 
29      select H,N
30     union all 
31      select J,N
32     union all 
33      select K,N
34     union all 
35      select L,N
36     union all 
37      select M,N
38     union all 
39      select N,N
40     union all 
41      select O,N
42     union all 
43      select P,N
44     union all 
45      select Q,N
46     union all 
47      select R,N
48     union all 
49      select S,N
50     union all 
51      select T,N
52     union all 
53      select W,N
54     union all 
55      select X,N
56     union all 
57      select Y,N
58     union all 
59      select Z,N) T 
60    where word >= @word 
61    collate Chinese_PRC_CS_AS_KS_WS order by PY ASC) 
62   --else @word 
63   else (case when CHARINDEX(@word,-0123456789)>1 and @word <>‘‘  then @word else ‘‘ end ) 
64   end) 
65   set @str = right(@str, len(@str) - 1) 
66  end
67  return @PY 
68 end
69 GO
获取中文单字拼音

SQLserver中通过首字母查询数据:

技术分享图片
 1 create function   f_GetPy(@str   nvarchar(4000)) 
 2 returns   nvarchar(4000) 
 3 as 
 4 begin 
 5 declare   @strlen   int,@re   nvarchar(4000) 
 6 declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 
 7 insert   into   @t(chr,letter) 
 8     select   , A    union   all   select   , B    union   all 
 9     select   , C    union   all   select   , D    union   all 
10     select   , E    union   all   select   , F    union   all 
11     select   , G    union   all   select   , H    union   all 
12     select   , J    union   all   select   , K    union   all 
13     select   , L    union   all   select   , M    union   all 
14     select   , N    union   all   select   , O    union   all 
15     select   , P    union   all   select   , Q    union   all 
16     select   , R    union   all   select   , S    union   all 
17     select   , T    union   all   select   , W    union   all 
18     select   , X    union   all   select   , Y    union   all 
19     select   , Z  
20     select   @strlen=len(@str),@re=   
21     while   @strlen> 0 
22     begin 
23         select   top   1   @re=letter+@re,@strlen=@strlen-1 
24             from   @t   a   where   chr <=substring(@str,@strlen,1) 
25             order   by   chr   desc 
26         if   @@rowcount=0 
27             select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 
28     end 
29     return(@re) 
30 end 
31  
32  
33 ---查询---
34 select 
35   * 
36 from 
37   [pactinfo] --表名
38 where
39   left(dbo.f_GetPy(pactname),1)=Z --可以把1改成任何数字 --pactname 字段
通过文字首字母查询数据

SQLserver中获得文字首字母

技术分享图片
 1 create function [dbo].[fun_getPY](@str nvarchar(4000)) 
 2 returns nvarchar(4000) 
 3 as 
 4 begin 
 5 declare @word nvarchar(1),@PY nvarchar(4000) 
 6 set @PY=‘‘
 7 set @str = ltrim(rtrim(@str)) 
 8 while len(@str)>0 
 9 begin 
10 set @word=left(@str,1) 
11 
12 set @PY=@PY+ltrim((case when unicode(@word) between 19968 and 19968+20901 
13 then (select top 1 PY from ( 
14 select A as PY,N as word 
15 union all select B,N簿 
16 union all select C,N 
17 union all select D,N 
18 union all select E,N 
19 union all select F,N 
20 union all select G,N 
21 union all select H,N 
22 union all select J,N 
23 union all select K,N 
24 union all select L,N 
25 union all select M,N 
26 union all select N,N 
27 union all select O,N 
28 union all select P,N 
29 union all select Q,N 
30 union all select R,N 
31 union all select S,N 
32 union all select T,N 
33 union all select W,N 
34 union all select X,N 
35 union all select Y,N 
36 union all select Z,N 
37 ) T 
38 where word>=@word collate Chinese_PRC_CS_AS_KS_WS
39 order by PY ASC)
40 when unicode(@word) between 8544 and 8552  --希腊字母1-9
41 then  nchar(unicode(@word)-8495)
42 when unicode(@word) = 8553  --希腊字母10
43 then 0
44 when  unicode(@word) between 48 and 57   --数字0-9
45 then @word 
46 when  unicode(@word) between 65296 and 65305   --全角数字0-9
47 then nchar(unicode(@word)-65248) 
48 when  unicode(upper(@word)) between 65 and 90   --字母a-z和A-Z
49 then upper(@word) 
50 when  unicode(upper(@word)) between 65313 and 65338  --全角字母a-z和A-Z
51 then nchar(unicode(upper(@word))-65248)
52 else ‘‘ end))   --如果非汉字字符或非字母、数字、希腊字母、全角字母、全角数字,返回空字符 
53 --set @str=right(@str,len(@str)-1)
54 set @str = substring(@str,2,len(@str)-1)
55 end 
56 return @PY 
57 end 
58 
59 --查询
60 select dbo.fun_getPY((SELECT NAME FROM GOODS where ID =11901)) as result
获得文字首字母

一般函数的使用方式:

--根据首字母查询
select  * from   GOODS where  left(dbo.f_GetPy(SPECS),2)=‘20‘
--获得字段文字的首字母
select dbo.fun_getPY((SELECT NAME FROM GOODS where ID =‘11901‘)) as result
--获得文字的首字母
select dbo.f_GetPinyin(‘张三‘) as result


函数的参数可以换成相应的数据字段

  

2019-2-14SQLserver中拼音查询数据

原文:https://www.cnblogs.com/liuqifeng/p/10374431.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!