一个表:
有id,h_add字段,h_add是字符型,
存了像这样的数据 
2
3
2,3
3,4
我现在想从表中查出h_add列中等于","前的数据或者等于","后的数据,用一个sql能不能实现?

解决方案 »

  1.   

    例:
    select * from 表名 where charindex(',2,',','+h_add+',')>0
      

  2.   

    declare @t table(code varchar(10))
    insert into @t select '2'
    insert into @t select '3'
    insert into @t select '2,3'
    insert into @t select '3,4'declare @code varchar(10)
    set @code='2'select * from @t where charindex(','+@code+',',','+code+',')>0
      

  3.   

    declare @t table(code varchar(10))
    insert into @t select '2'
    insert into @t select '3'
    insert into @t select '2,3'
    insert into @t select '3,4'declare @code varchar(10)
    set @code='2'select * from @t where charindex(','+@code+',',','+code+',')>0/*
    code       
    ---------- 
    2
    2,3
    */
      

  4.   


    select left('3,4',charindex(',','3,4',len('3,4')-2)-1)
    select substring('3,4',charindex(',','3,4')+1,6)
      

  5.   

    select substring(h_add,1,charindex(',',h_add)+1), substring(h_add,charindex(',',h_add)+1,6) from table
      

  6.   


    select substring(h_add,1,charindex(',',h_add)+1),case when charindex(',',h_add)=0 then '' else  right(h_add,len(h_add)-charindex(',',h_add)) end
    from table