代码之家  ›  专栏  ›  技术社区  ›  Dan Andreatta

选择字符串比较作为tsql中的布尔值

  •  1
  • Dan Andreatta  · 技术社区  · 14 年前

    在tsql查询中,我希望有一个计算字段,它是字符串比较的布尔结果。

    它看起来像这样:

    select name, (status = 'current') as IsValid
    from items
    

    但我列出的查询无效。正确的语法是什么?

    3 回复  |  直到 14 年前
        1
  •  8
  •   cmsjr    14 年前

    我会用案例陈述

    Select name, case when status = 'current' then 1 else 0 end as IsValid
    from items
    
        2
  •  4
  •   Sachin Shanbhag    14 年前

    试试这个-

    select name, 
    (CASE status WHEN 'current' THEN 1 Else 0 END) as IsValid 
    from items
    
        3
  •  1
  •   Yury Chaikou    9 年前
    select name, IIF(status = 'current', 1, 0) as IsValid
    from items