declare @tab table(id int,name varchar(20))
insert @tab values(1,'12345')
insert @tab values(2,'aa')
insert @tab values(3,'hhh45')
insert @tab values(4,'dd09')select * from @tab where
(charindex('a',name)>0) or
(charindex('d',name)>0)
select * from @tab where
(charindex('1',name)>0) or
(charindex('9',name)>0)
insert @tab values(1,'12345')
insert @tab values(2,'aa')
insert @tab values(3,'hhh45')
insert @tab values(4,'dd09')select * from @tab where
(charindex('a',name)>0) or
(charindex('d',name)>0)
select * from @tab where
(charindex('1',name)>0) or
(charindex('9',name)>0)
id f1
1 **
2 **
3 **
.. ..想查询id为指定值v得记录,如id =1,5,7等,关键是v的值有多个,且不固定,请问怎么做?
go
insert into aa select 1,'aa'
union all select 2,'bb'
union all select 3,'cc'
union all select 4,'dd'
union all select 5,'ee'
union all select 6,'ff'
union all select 7,'gg'
union all select 8,'hh'
union all select 9,'ii'
go
declare @nums varchar(50)
set @nums='1,5,7'
set @nums=','+@nums+','
select * from aa where charindex(','+cast(a as varchar(10))+',',@nums)>0
--测试结果
a b
----------- --------------------
1 aa
5 ee
7 gg(所影响的行数为 3 行)