表中有两个字段fld0、fld1 和fld2其值:
fld0 fld1   fld2
 AB     1       2  
 AA     1       2  
 AB     1       2  
我想在显示查询时当fld0的值中有B时fld2 as x,否则fld1 as x.该语句怎么样写?

解决方案 »

  1.   

    select  fld0 , case when charindex('B',fld0 )  >0 then fld2  else  fld1 end as x
    from 
      

  2.   

    select case when fld0 like '%B%' then x else fld2 end fld2,
           case when fld0 not like '%B%' then x else fld1 end fld1
    from tb
      

  3.   

    这段代码效率较低,因为把字段放到函数里会迫使SQL放弃索引,进行全盘扫描。建议采用(#10楼)
    select case when fld0 like '%B%' then x else fld2 end fld2,
      case when fld0 not like '%B%' then x else fld1 end fld1
    from tb where fld0 like '%B%'