34,588
社区成员
发帖
与我相关
我的任务
分享
--优化一下,用嵌套的replace更好,
select * from @tb
where replace(replace(replace(replace(replace(replace(','+name+',',' ',''),--去空格
',001,',',,'), --去掉001
',002,',',,'), --去掉002
',003,',',,'), --去掉003
',004,',',,'),--最后去掉逗号
',','')
='' --等于空格时即为符合条件
select * from table where charindex(name,'001,002,003,004')!=0
select * from @tb
where case when charindex('001',name,1)>0 then len('001') else 0 end
+case when charindex('002',name,1)>0 then len('002') else 0 end
+case when charindex('003',name,1)>0 then len('003') else 0 end
+case when charindex('004',name,1)>0 then len('004') else 0 end=len(replace(replace(name,',',''),' ',''))
select * from table where name in ('001','002','003','004')