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!
SQL Server IIF() FunctionThe IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.
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.
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 ...
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With