IIF statement in SQL Server 2005

Zerotoinfinity picture Zerotoinfinity · Dec 7, 2010 · Viewed 17k times · Source

Does IIF statement exists in all version of SQL Server ?

I have checked a tutorial on MSDN.

But when I tried to run this code on my machine

DECLARE @newDate datetime
SET @newDate =  CONVERT(varchar, {fn NOW()}, 111)
SELECT IIF(@newDate > '2010/12/2', 'Greater', 'smaller')

But I am getting error of "Incorrect syntax near '>'."

Can someone provide me an example in SQL Server 2005 for the existence of the IIF statement?

Answer

marc_s picture marc_s · Dec 7, 2010

That IIF statement only exists in MDX - the query language for SQL Server Analysis Services - the datawarehousing side of SQL Server.

Plain T-SQL does not have an IIF statement.

The best you can do in T-SQL is use the CASE.... WHEN... THEN... statement.