Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a ternary conditional operator in T-SQL?

People also ask

Is there a ternary operator in SQL?

There's no ternary operator in T-SQL.

What does != Mean in SQL?

Not Equal Operator: != Evaluates both SQL expressions and returns 1 if they are not equal and 0 if they are equal, or NULL if either expression is NULL. If the expressions return different data types, (for instance, a number and a string), performs type conversion.

How do you write a 3 condition ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Why we should not use ternary operator?

They simply are. They very easily allow for very sloppy and difficult to maintain code. Very sloppy and difficult to maintain code is bad. Therefore a lot of people improperly assume (since it's all they've ever seen come from them) that ternary operators are bad.


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