SQLTeam.com | Weblogs | Forums

Example for IIF doesnt work when copy/paste


#1

From SSMS when I enter

DECLARE @a int = 45, @b int = 40;
SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result;

I get
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '>'.

But I copy pasted directly out of https://docs.microsoft.com/en-us/sql/t-sql/functions/logical-functions-iif-transact-sql

Why is that ?? Thanks


#2

Most probably you are using a version of SQL that is too old. IIF() was introduced in SQL 2012

Check your version:

SELECT @@version

#3

Yes, my version is 2008 - waiting to be updated.... Thanks !!!


#4

2008 supports CASE:

DECLARE @a int = 45, @b int = 40;

SELECT CASE WHEN @a > @b THEN 'TRUE' ELSE 'FALSE' END AS Result;

#5

Thanks so much !!!