In this article, We will learn about the concept of the IIF() function in SQL servers.
IIF() Function
- IIF() function introduced in SQL server 2012.
- This function returns the value on the basis of a condition or expression written in the function.
- IIF() function compose of the logical statement, the boolean expression that is known as the condition which is followed by a true and false expression.
- IIF() function is the same as the WHEN and CASE expression. We can say that IIF() function is the shortened way to write CASE expression.
Syntax
IIF(boolean_expressions, value_if_true, value_if_false) -- OR IIF(condition, value_if_true, value_if_false)
Here IIF() function has three parameters as below,
Condition: It’s required. The expression that we have to check.
[ Note: If this argument is not a boolean expression then a syntax error is raised.]
value_if_true: It’s optional. The value that we want to return if the condition is true.
value_if_false: It’s optional. The value that we want to return if the condition is false.
Example
select e.EmployeeName ,e.City ,e.Salary ,IIF(e.Gender = 'm','Male','Female') Gender From tbl_Employee e
In the above example, “IIF(e.Gender = ‘m’, ‘Male’, ‘Female’) Gender” will return the value ‘Male’ if the condition is true and it will return the value ‘Female’ if the condition is false.
Output,
I hope you guys found this useful article and help you to understand the concept of the IIF() function in SQL servers.
Thank you.