Today, we are going to see IIF function in SQL Server. Honestly speaking, I have never used this function in any of my code so far just because I am very much good with CASE WHEN expressions. However, let us look at some of the usages of IIF in details as part of our learning.
Syntax – IIF(condition,value_if_condition_is_true,value_if_condition_is_false)
Declare @Age int =18
Select IIF(@Age >= 18, 'Adult','Kid')
--Equivalent CASE WHEN representation
Select CASE WHEN @Age >= 18 THEN 'Adult' ELSE 'Kid' END
Remarks
1. SQL Server internally converts IIF to CASE WHEN expression, hence the rules applies to CASE WHEN expression applies to IIF function as well.
2. It is to be noted that either of 2nd and 3rd parameter should be a NON NULL value.
I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!