Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 IIF statement does not seem enabled

I am trying to use IIF() in a select statement. The boolean expression checks to see if a fields value is equal to an empty string. The syntax is like so:

SELECT IIF(field = '','ONe action','Another') 

I am getting the error "syntax error near ="

I tried a simple test:

SELECT IIF(2 > 1, 'yes','no') 

and I am getting "syntax errror near >"

This is leading me to believe that IIF is not working at all.

I am using SQL SERVER 2008 R2, is there something that needs to be configured to allow IIF() to work? Is there something about the syntax that I am missing? My test is simple as can be and I still get the syntax error.

Any help would be appreciated. Thanks much!

like image 268
TheMethod Avatar asked Jul 18 '12 11:07

TheMethod


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.

Which is better case or IIF in SQL Server?

IIF is one of the logical Transact-SQL function which returns one among the two values, based on the boolean expression. On the other hand, CASE is a language expression which evaluates a list of conditions and returns one among multiple values, based on the boolean expression.

What is the difference between IF and IIF?

The critical difference between IIF (available from VS 2002 forward) and IF (available in VS 2005 forward) is that IIF is a function and evaluates all of its arguments prior to returning a value, while IF is an operator that executes like a short-circuiting conditional, only evaluating the true or false argument ...

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).


1 Answers

As noted, IIF is a SQL2012 feature.

Replace your IIF with a CASE ( Which is what SQL 2012 would do anyway )

 SELECT CASE field WHEN '' THEN 'ONe action' ELSE 'Another' END 
like image 189
podiluska Avatar answered Oct 14 '22 08:10

podiluska