IIF Command T-SQL

IIF is simply shorthand for a CASE statement.

IIF was introduced in SQL 2012 and you may find it in the 70-461 exams.

Previous way to do a case statement

-----------------------------------------
SELECT CASE WHEN 5 > 6 THEN 'False' ELSE 'TRUE' END as Answer
-----------------------------------------

Same statement using IIF

-----------------------------------------
SELECT IIF( 5 > 6 , 'False' ,'TRUE') as Answer
-----------------------------------------

Microsoft might be patting themselves on the back for introducing this, but it looks to me like the guys on the excel and access teams figured it out years ago!

IIF in Excel

You can also nest your IIF statements, however code can quickly become complicated and cumbersome if you try to get to tricky.

Here at DBAFire.com we belive in keeping things simple, scalable and fast.

If you prefer to watch video rather than read text, here is a video by Steve Stedman demonstrating the IIF statement.

IIF in SQL Server 2012/2014

We hope you found this article quick, and helpful.

Rob StGeorge
Senior SQL Server Database Administrator residing in Auckland, NZ

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.