There's no ternary operator in T-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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With