Is there a ternary conditional operator in T-SQL?

Sql ServerTsql

Sql Server Problem Overview


What are alternatives to implement the following query:

select *  
from table  
where isExternal = @type = 2 ? 1 : 0

Sql Server Solutions


Solution 1 - Sql Server

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#)

Solution 2 - Sql Server

Use case:

select *
from table
where isExternal = case @type when 2 then 1 else 0 end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnthonyView Question on Stackoverflow
Solution 1 - Sql Servermarc_sView Answer on Stackoverflow
Solution 2 - Sql ServerGuffaView Answer on Stackoverflow