如:这个表中有几个字段值都含有123,当传递123时,把值包含123的都查询出来。

解决方案 »

  1.   

    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 wselect * from #table select * from #table where a+b like '%123%'
    select * from #table where CHARINDEX('123', a+b)>0
      

  2.   


    select * 
    from tb
    where col1 like '%123%' or col2 like '%123%' or col3 like '%123%'
    或者
    select * 
    from tb
    where charindex('123',col1)>0 or charindex('123',col2)>0 or charindex('123',col3)>0 
      

  3.   

    如果列不多,那就一个一个列写,如果列很多,那就动态拼接sql
      

  4.   

    declare @i int 
    set @i =convert(varchar(10),@i)convert 进行转换
      

  5.   

    select * from #table where CHARINDEX('123', convert(varchar(10),a)+convert(varchar(10),b))>0