首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查询字符中是不是包含字符串中的某值 ,号分隔

2012-08-03 
查询字符中是否包含字符串中的某值 ,号分隔查询表tableA 中的info字段,是否包含了 如1,3,5中的其中一个

查询字符中是否包含字符串中的某值 ,号分隔
查询表tableA 中的info字段,是否包含了 如"1,3,5"中的其中一个数值

info字段的内容也是 1,2,3这种数字加,的结构

注意:sql语句中,不可手工折解1,3,5字符

[解决办法]

SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([id] int,[info] varchar(6))goinsert [test]select 1,'1,2,3' union allselect 2,'1,3' union allselect 3,'4,5,6' union allselect 4,'2,4,16'goif OBJECT_ID('pro_test')is not nulldrop proc pro_testgocreate proc pro_test(@str varchar(20))ascreate table #test(id int identity,value int)--declare @str varchar(1000)--set @str='1,2,3'declare @sql varchar(1000)set @sql='insert #test(value)           select '+REPLACE(@str,',',' union all select ')print @sqlexec(@sql)select     distinct a.*from     test ainner join     #test bon    CHARINDEX(','+LTRIM(b.value)+',',''+a.[info]+',')>0go--主意,我理解的你的包含是指info字段同时包含所给字符串中的没一个数字exec pro_test '1,3,4'/*id    info1    1,2,32    1,34    2,4,16*/--再不是这样就只有帮顶帖子了 

热点排行
Bad Request.