Is there a ternary conditional statement in T-SQL?

What are the alternatives for implementing the following query:

select * from table where isExternal = @type = 2 ? 1 : 0 
+94
sql-server tsql
Apr 25 '13 at 8:21
source share
2 answers

Use case :

 select * from table where isExternal = case @type when 2 then 1 else 0 end 
+106
Apr 25 '13 at 8:22
source share

In SQL Server 2012, you can 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 simply = (and not == - this is C #)

+133
Apr 25 '13 at 8:35
source share



All Articles