Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIF(...) not a recognized built-in function

I'm trying to use this in Microsoft SQL Server 2008 R2:

SET @SomeVar = @SomeOtherVar +   IIF(@SomeBool, 'value when true', 'value when false') 

But I get an error:

IIF(...) is not a recognized built-in function name

Is IIF() only compatible with a later version?

Is there an alternate function I can use?

like image 806
Danny Beckett Avatar asked Aug 20 '12 09:08

Danny Beckett


People also ask

What is the limitation of IIF () function?

As mentioned, the IIF() function is based on the CASE expression, and therefore has the same limitations of the CASE expression (such as only being able to nest to a maximum level of 10).

What is IIF function in SQL?

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.

What is IIF condition?

You use IIf to determine if another expression is true or false. If the expression is true, IIf returns one value; if it is false, IIf returns another. You specify the values IIf returns. See some examples. Syntax.


2 Answers

IIF comes from SQL 2012. Before then, you can use CASE:

SET @SomeVar = @SomeOtherVar + CASE  WHEN @SomeBool  THEN 'value when true'  ELSE 'value when false' END 
like image 57
Richard Avatar answered Sep 17 '22 16:09

Richard


What's New in SQL Server 2012, Programmability Enhancements:

SQL Server 2012 introduces 14 new built-in functions. These functions ease the path of migration for information workers by emulating functionality that is found in the expression languages of many desktop applications. However these functions will also be useful to experienced users of SQL Server.

...

  • IIF (Transact SQL)
like image 28
Damien_The_Unbeliever Avatar answered Sep 20 '22 16:09

Damien_The_Unbeliever