Is there a ternary conditional operator in T-SQL?
What are alternatives to implement the following query:
select *
from table
where isExternal = @type = 2 ? 1 : 0
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#)
Use case
:
select *
from table
where isExternal = case @type when 2 then 1 else 0 end