Life Is Not IIF()
Talk To Me • Y'Akoto • 2012
• T-Sql
I discovered the IIF() function with Tableau Software. It’s really simple to use. It returns a value if a condition is TRUE, or another value if a condition is FALSE.
- Syntax:
IIF(boolean_expression, value_if_true, value_if_false)SELECT OrderID, Quantity, IIF(Quantity>10, 'MORE', 'LESS') FROM OrderDetails; IFFcan be nested and fill with variablesSELECT IIF(@Person = 'Raj', 'Likes Apple', IIF(@Person = 'Vinay', 'Likes Orange', 'Person does not exists in the list') ) AS Result;Sometimes, data types might be different in the result conditions. SQL Server gives the hightest precedencce data type. If
Trueis float andFalseis integer, the result for both will be a float. I will go deeper with this topic later.
A similar result can be achieved using a CASE statement as well, IIF() is a shorthand way for writing a CASE expression.
And that’s all!
So, step by step and keep learning!