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