SQL Server Tutorial: SQL Server IIF Logical Function With Example
This article will demonstrate the logical built-in IIF function introduced in SQL Server 2012. The IIF function in SQL Server is a logical function that returns one of two values based on the evaluation of a Boolean expression. The IIF() function is used to test the if else condition. It takes 3 parameters. The first parameter is a boolean expression based on the first parameter. It will have the same result as the second and third parameters. If the boolean expression is true, then the second parameter returns true. If the boolean expression is false, the third parameter will return as false.
IIF() is the shorthand writing of a case statement in SQL Server. It supports 10 nested IIF().
Syntax of IIF()
boolean_expression
The first parameter is a boolean expression. Based on a boolean expression, evaluate and return the second and third parameter results.
true_value
IIF() returns true when a boolean expression evaluates to true.
false_value
IIF() returns a false value when a boolean expression evaluates to false.
It works similar to a case statement. Let’s take Case statement syntax,
Example of IIF() and Case statement: True result
Example of IIF() and Case statement: False result
We should be cautious while using NULL values with the IIF function. We can use only one NULL value in the 2nd or 3rd parameter. If you use both parameters as null, then it will throw an error.