Share via


how to use iif in sql server 2008?

Question

Friday, July 20, 2012 7:54 AM | 1 vote

hi all

i used iif from this refrence

http://msdn.microsoft.com/en-us/library/hh413574.aspx

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

but i see this error:

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '>'.

how to solve this problem

thanks

Name of Allah, Most Gracious, Most Merciful and He created the human

All replies (4)

Friday, July 20, 2012 7:58 AM ✅Answered | 5 votes

Take a closer look at the link. IIF() is new and only available for SQL Server 2012.

It's a shorthand for CASE as described in the link also. So use

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

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

btw, change the thread type to question.


Friday, July 20, 2012 8:02 AM ✅Answered | 2 votes

Try CASE STATEMNET as follows

DECLARE @a int=45DECLARE @b int=40SELECT         CASE          WHEN @A>@B THEN 'True'         ELSE 'FALSE'               END

For more information please look at http://msdn.microsoft.com/en-us/library/ms181765.aspx

Thanks
Manish

Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.


Friday, July 20, 2012 8:07 AM ✅Answered | 2 votes

Also consider this way

..

SELECT case when @a > @b then 'TRUE' else 'FALSE' end AS Result;

Many Thanks & Best Regards, Hua Min


Friday, January 27, 2017 3:18 AM

Thank you!