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 others have said, 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
is not valid for SQL Server 2008 R2 and any version before that.
IIF
was introduced in SQL Server 2012 (there is no link to previous versions on the documentation page I have linked to).
You could also use the standard IF statement if it's outside a select.
E.g.
DECLARE @Answer VARCHAR(3) = 'YES'
IF @Answer = 'Yes'
BEGIN
--Do Something if true
END
ELSE
-- Do Soemthing if false