我有一个字段是bit的,我想在查询时判断其值,如果为0则返回一个固定的字符串,为1则返回另一个字符串我应当如何做? 
A表如下:
Content,Caption,bit,PkID
根据PkID来返回数据表,bit为1返回字符串'0',bit为0时返回字符串'1'

解决方案 »

  1.   

    selecct case 字段 when 0 then '1' when 1 then '0' else 'null' end from 表
      

  2.   

    select case [bit]
    when 1 then '0'
    when 0 then '1'
    end
    from tablename
    where pkid in (...)
      

  3.   

    Declare @t table(Content varchar(800),caption varchar(50),AAA bit,pkid int)
    Insert @t select 'aaa','ddd',0,1
    Union all select 'bbb','eee',1,2
    Union all select 'ccc','ttt',0,2
    --------------
    Select case when AAA=0 then '字段AAA为0时的字符串' else '字段AAA为1时的字符串' end as 字符串
    From @t
      

  4.   

    喜欢用NULLIF和ISNULL。呵呵
    DECLARE @a TABLE(x bit)
    INSERT @a SELECT 1 UNION ALL SELECT 0 UNION ALL SELECT 1
    SELECT x,ISNULL(STUFF(RTRIM(NULLIF(x,1))+'为0时我显示',1,1,''),'为1时我显示') FROM @a