SQL Server 2008 IIF statement does not seem enabled

TheMethod picture TheMethod · Jul 18, 2012 · Viewed 77.3k times · Source

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!

Answer

podiluska picture podiluska · Jul 18, 2012

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