表 Table1
字段 id name
1,2,3 abc
4,5,12 cde比如所 我现在需要 得到 id 里包含 2 的 name
我想到用 select [name] from Table1 where charindex('2',id)<>0 但是 这样 12 也满足条件 请各位大大赐教
字段 id name
1,2,3 abc
4,5,12 cde比如所 我现在需要 得到 id 里包含 2 的 name
我想到用 select [name] from Table1 where charindex('2',id)<>0 但是 这样 12 也满足条件 请各位大大赐教
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-06-17 10:32:00
----------------------------------------------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
create table [table1]([id] varchar(10),[name] varchar(3))
insert [table1]
select '1,2,3','abc' union all
select '4,5,12','cde'
--------------开始查询--------------------------
select [name] from Table1 where charindex(',2,',','+id+',')>0
----------------结果----------------------------
/*name
abc
*/
insert into #tb values ('1,2,3','abc')
insert into #tb values ('4,5,12','cde')
insert into #tb values ('1,12,3','def')
insert into #tb values ('1,24,3','defg')
insert into #tb values ('22,24,3','defgh')select [name] from #tb where charindex(',2,',','+id+',')>0
id name
1,2,3 abc
4,5,12 cde
24 asd <== 这里面没有','哦
这样呢?