Solved

IIF function in sql server

Posted on 2004-08-18
7
2,519 Views
Last Modified: 2008-11-17
Hi Freinds,
Please help me with the IIF function in sql
Getting syntax error for following line at = sign

iif (cast(rcv_stats_shelter_plus_chk_c as int)=1, 1, 0)

The query is returning as syntax error near =

Really frustrated with this. Please help

Thanks,
Rup
0
Comment
Question by:rk75
7 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 20 total points
Comment Utility
you may want a case statement :

case cast(rcv_stats_shelter_plus_chk_c as int)
when 1 then 1
else 0
end
0
 
LVL 10

Expert Comment

by:Jay Toops
Comment Utility
its returning an error because IIF is not supported with sql server ..

so you have to use the case statement AND...
you can INLINE this case statement with regular sql
you might want to add " AS FIELDNAME" IMMediately after the END

example
case cast(rcv_stats_shelter_plus_chk_c as int)
when 1 then 1
else 0
end as MYFIELDNAME
0
 
LVL 3

Expert Comment

by:eekj
Comment Utility
Write your own immediate if function:

CREATE  FUNCTION iif
    (@Expr AS int, @TruePart AS int, FalsePart AS int)

RETURNS int

BEGIN
    if @Expr = 1 then   --some databases such as mysql evaluate an expression to 0=false 1=true
        return @TruePart
    else
        return @FalsePart
END
0
 

Author Comment

by:rk75
Comment Utility
Thnaks friends,
I used case.... when.... then....else....end
in the place of iif
And it worked.

Thanks for your time and help
RK
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Thanks for your time and help<<
Than this is the point where you close the question by awarding points.  See here for more info:
How do I accept a comment?
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi68
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now