如何在一个表中对某个值进行模糊查询,返回整个行的值集合。
如:这个表中有几个字段值都含有123,当传递123时,把值包含123的都查询出来。
[解决办法]
create table #table(a varchar(10),b varchar(10),c varchar(10) )
delete from #table
insert into #table
select * from (
select 1 as 'a',123 as 'b',123 as 'c'
union
select 2 as 'a',123455 as 'b',12 as 'c'
union
select 3 as 'a',12 as 'b',1 as 'c'
union
select 4 as 'a',123455 as 'b',123 as 'c'
) as w
select * from #table
select * from #table where a+b like '%123%'
select * from #table where CHARINDEX('123', a+b)>0
[解决办法]
declare @i int
set @i =convert(varchar(10),@i)
convert 进行转换
[解决办法]
select * from #table where CHARINDEX('123', convert(varchar(10),a)+convert(varchar(10),b))>0