博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL中根据汉字的拼音首字母模糊查询
阅读量:4359 次
发布时间:2019-06-07

本文共 2086 字,大约阅读时间需要 6 分钟。

1 ---测试数据---  2 ifobject_id('[pactinfo]') isnotnulldroptable[pactinfo]  3 go  4 createtable[pactinfo]([ID]int,[pactname]varchar(4))  5 insert[pactinfo]  6 select1,'正常'unionall  7 select2,'中国'unionall  8 select3,'做饭'unionall  9 select4,'加发' 10 11 ---引用前辈们的一个函数--- 12 createfunction   f_GetPy(@str   nvarchar(4000)) 13 returns   nvarchar(4000) 14 as 15 begin 16 declare   @strlen   int,@re   nvarchar(4000) 17 declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 18 insert   into   @t(chr,letter) 19     select   '吖 ', 'A '   union   all   select   '八 ', 'B '   union   all 20     select   '嚓 ', 'C '   union   all   select   '咑 ', 'D '   union   all 21     select   '妸 ', 'E '   union   all   select   '发 ', 'F '   union   all 22     select   '旮 ', 'G '   union   all   select   '铪 ', 'H '   union   all 23     select   '丌 ', 'J '   union   all   select   '咔 ', 'K '   union   all 24     select   '垃 ', 'L '   union   all   select   '嘸 ', 'M '   union   all 25     select   '拏 ', 'N '   union   all   select   '噢 ', 'O '   union   all 26     select   '妑 ', 'P '   union   all   select   '七 ', 'Q '   union   all 27     select   '呥 ', 'R '   union   all   select   '仨 ', 'S '   union   all 28     select   '他 ', 'T '   union   all   select   '屲 ', 'W '   union   all 29     select   '夕 ', 'X '   union   all   select   '丫 ', 'Y '   union   all 30     select   '帀 ', 'Z ' 31     select   @strlen=len(@str),@re='' 32     while   @strlen>0 33     begin 34         select   top   1   @re=letter+@re,@strlen=@strlen-1 35             from   @t   a   where   chr <=substring(@str,@strlen,1) 36             order   by   chr   desc 37         if   @@rowcount=0 38             select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 39     end 40     return(@re) 41 end 42 43 44 ---查询--- 45 select 46   * 47 from 48   [pactinfo] 49 where 50   left(dbo.f_GetPy(pactname),1)='Z' 51 52 ---结果--- 53 ID          pactname 54 ----------- -------- 55 1           正常 56 2           中国 57 3           做饭

 

转载于:https://www.cnblogs.com/lilwzca/archive/2011/11/17/2253020.html

你可能感兴趣的文章
珍藏的最全的windows操作系统快捷键
查看>>
【DBAplus】SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
查看>>
二叉树结点删除操作
查看>>
图论-单源最短路-SPFA算法
查看>>
转换文件的字符集
查看>>
prometheus + grafana安装部署(centos6.8)
查看>>
Redis和Memcached的区别【转】
查看>>
VMware: Deploy multiple VM’s from template with PowerCLI
查看>>
Cascaded pose regression
查看>>
model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转;解决的方法是用responsewrite(普通的字符响应)...
查看>>
自动在数据库中创建表
查看>>
如何在一个进程中启动另外一个线程:ProcessStartInfo Constructor
查看>>
树状数组模板题 P1904
查看>>
Kerberos安装及使用
查看>>
android 布局中 layout_gravity、gravity、orientation、layout_weight
查看>>
highcharts
查看>>
【学员管理系统】0x02 学生信息管理功能
查看>>
什么是Entity Framework(ORM)
查看>>
软件质量理解
查看>>
jquery 在 table 中修改某行值
查看>>