比如有如下2个字段的数据
name    role
a       0,2,3,5
b       0
c       0,21,57
d       0,12
e       0,5,2
如何查询出role字段里 等于2的数据

解决方案 »

  1.   

    select * from tb1 where ','+role+',' like '%,2,%'
      

  2.   

    select * from [table] where charindex(',2,',','+role+',')>0
      

  3.   

    select * from #a where CharIndex(',2,',role)>0 UNION --在字段中間 如 0,2,3,5
    select * from #a where  CharIndex('2,',role)=1 UNION --在第一位的 如 2,3
    select * from #a where  CharIndex(',2',role)>0 and (CharIndex(',2',role)= len(role)-1) --在最後一位的 如 3,2
      

  4.   

    SELECT [name] FROM t WHERE PATINDEX( '%,2,%' , ','+role+',' )>0name       
    ---------- 
    a
    e