Is there a ternary conditional operator in T-SQL?

后端 未结 2 1803
青春惊慌失措
青春惊慌失措 2021-01-30 01:25

What are alternatives to implement the following query:

select *  
from table  
where isExternal = @type = 2 ? 1 : 0
相关标签:
2条回答
  • 2021-01-30 01:47

    In SQL Server 2012, you could use the IIF function:

    SELECT *
    FROM table
    WHERE isExternal = IIF(@type = 2, 1, 0)
    

    Also note: in T-SQL, the assignment (and comparison) operator is just = (and not == - that's C#)

    0 讨论(0)
  • 2021-01-30 01:51

    Use case:

    select *
    from table
    where isExternal = case @type when 2 then 1 else 0 end
    
    0 讨论(0)
提交回复
热议问题