Stephanie Chatagner's Blog

• T-Sql

Well.

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;
    
  • IFF can be nested and fill with variables
      SELECT 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 True is float and False is 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!