表A:
ID  病名
1   伤寒,呕吐
2   咳嗽,伤寒一日要求:查出含有“伤寒”的记录
如果用charindex,把“伤寒一日”也查出来,用什么办法只差包含“伤寒”的记录

解决方案 »

  1.   

    WHERE ',' + 病名 + ',' LIKE '%,伤寒,%'OR 
    WHERE CHARINDEX(',伤寒,',',' + 病名 + ',')>0
      

  2.   

    select *from tb where charindex(',伤寒,',',' + 病名 + ',')>0也可以用patindex
      

  3.   

    create table 表A
    (
    ID int,
    病名 varchar(50))
    insert into 表A values(1,'伤寒,呕吐')
    insert into 表A values(2,'咳嗽,伤寒一日')
    select * from 表A where 病名 like '%伤寒,%'
    select * from 表A where  charindex ('伤寒,',病名)>0
    ID          病名
    ----------- --------------------------------------------------
    1           伤寒,呕吐(1 行受影响)
      

  4.   


    declare @t table (id int,病名 varchar(14))
    insert into @t
    select 1,'伤寒,呕吐' union all
    select 2,'咳嗽,伤寒一日'select * from @t where charindex(',伤寒,',','+病名+',')>0
    /*
    id          病名
    ----------- --------------
    1           伤寒,呕吐
    */