sqlrx logo
SQLRx® Tip of the Month
 

February 2013
______________________________________________________________________________
wrench   SQL Server Development: SQL 2012 IIF() Function and CASE Expression
 
 A new function available in SQL Server 2012 is IIF. IIF is very similar to the CASE expression and is in essence a shorthand way to write a CASE function. The IIF function returns one of two values depending on whether the first expression evaluates to TRUE or FALSE. The IIF function can be nested up to 10 levels just like the CASE expression. IIF can only evaluate two values and no more.
  
Here is a simple example of each:
  

-- IIF

DECLARE @a int = 2012, @b int = 2013;

SELECT IIF( @a > @b, 'Out with the old.', 'In with the new.' ) AS HappyNewYear;

GO

 

-- CASE

DECLARE @a int = 2012, @b int = 2013;

SELECT (CASE WHEN @a > @b THEN 'Out with the old.' ELSE 'In with the new.' END) AS HappyNewYear;

GO

 

For more information:

http://msdn.microsoft.com/en-us/library/hh213574.aspx

http://msdn.microsoft.com/en-us/library/ms181765.aspx

______________________________________________________________________________
As always, don't forget to regularly monitor the 11 VitalSigns  used by SQLRx to debug performance bottlenecks.

If you have a SQL question you'd like us to answer, and possibly use for the next SQLRx Tip of the Month, email it to us!  If you missed any of our previous tips, you can view them here.

For assistance or more information on optimizing your SQL Server Environment, visit us at www.sqlrx.com or email Lori Brown at lorib@isi85.com.


Sincerely,

 

Lori Brown | SQLRx Senior Consultant | Integrated Services, Inc.
4144 N. Central Expwy, Suite 430  |  Dallas, TX  75204
Phone: 214.526.7680 x 113 | lorib@isi85.com 

 

 

Integrated Services, Inc.                                    
2008 winner logo small
Turning Data Into Profit

 

25 Years of Credibility & Trust

 

Join our email list          Check out our events          SQLRx Downloads