New SQL IIF Boolean Function in Transact-SQL with SQL Server 2012
SQL IIF function evaluates a boolean expression and according to the being True or False of the boolean expression, returns one of the input values. Application developers and programmers are familiar with IIF() function since many programming languages have IIF() boolean functions.
T-SQL IIF() boolean function is introduced to sql developes with SQL Server 2012, Denali CTP3. This new SQL IIF() function syntax is as follows:
IIF([boolean expression], [true value], [false value])
For T-SQL developers, IIF() function can be considered as a special type of SQL CASE() function. Let's make a sample SQL script to see how sql programmers can use iif() function instead of Case statement.
As you have expected the output of the above two functions are the same
SQL IIF() function has some additional behaviours that tsql developers should consider while coding especially related with NULL input parameters.
Although the below SELECT statements returns valid values from SQL IIF() functions,
The following IIF() function returns error message. Since the input values for TRUE and FALSE result of the boolean expression are both NULL, the SQL Engine throws the error message 8133.
Msg 8133, Level 16, State 1, Line 2
At lease one of the result expressions in a CASE specification must be an expression other than the NULL constant.
But if both TRUE and FALSE evaluation of the boolean expression resulting into variables which have NULL values, then the SQL IIF() function call executes successfully