比如 我有一个表 bumen
里面 有一个列 quanxian
id name quanxian
1 a 001;002;003;
2 b 002;003;
3 c 003;004;
4 d 001;002;003;
我的意思是把quanxian列中含有002的数据取出来;显示在gridview上?
请大侠们帮忙!
select * from tumen where quanxian like '%002%'
select * from tumen where quanxian like '%002%'
select * from tumen where charindex('002',quanxian) > 0
SQL code:
if object_id('ta') is not null drop table ta
go
create table ta(id int, name varchar(1), quanxian varchar(20))
insert into ta select
1, 'a', '001;002;003;' union all select
2, 'b', '002;003;' union all select
3, 'c', '003;004;' union all select
4, 'd', '001;002;003;'
select * from ta
where charindex(';002;',';'+quanxian)>0
id name quanxian
----------- ---- --------------------
1 a 001;002;003;
2 b 002;003;
4 d 001;002;003;
(3 行受影响)
SQL code:
select * from tumen where charindex(','+'002'+',',','+quanxian+',') >