Depending on whether the Boolean expression in SQL Server evaluates to true or false, the conditional function IIF returns one of two values. For ease of comprehension, the syntax and examples are provided below.
Syntax: IIF (boolean_expression, true_value, false_value)
Examples:
SELECT IIF( 1=2, ‘TRUE’, ‘FALSE’);
Result: FALSE
SELECT IIF( 1=2, ‘RIGHT’, ‘WRONG’)
Result: WRONG
Let’s see how it will be used in the table’s data. Consider the popular ’emp’ table.
SELECT Ename,
Sal,
IIF(Sal<1000,’Lower’,’Higher’),
Comm,
IIF(Comm IS NULL OR Comm=0,’None’,’Provided’)
FROM emp;
Result:
Hope you find this article helpful.
Please subscribe for more interesting updates.
2 comments