Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server IIF vs CASE

I recently came to know about the availability of IIF function in SQL Server 2012. I always use nested CASE in my queries. I want to know the exact purpose of the IIF statement and when should we prefer using IIF over CASE Statement in the query. I mostly use nested CASE in my queries.

Thanks for all the inputs.

like image 897
neophyte Avatar asked Apr 03 '14 13:04

neophyte


People also ask

Does IIF work in SQL Server?

SQL Server IIF() FunctionThe IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.

What is IIF SQL Server?

IIF is a shorthand way for writing a CASE expression. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation.


1 Answers

IIF is the same as CASE WHEN <Condition> THEN <true part> ELSE <false part> END. The query plan will be the same. It is, perhaps, "syntactical sugar" as initially implemented.

CASE is portable across all SQL platforms whereas IIF is SQL SERVER 2012+ specific.

like image 84
Karl Kieninger Avatar answered Oct 20 '22 08:10

Karl Kieninger