Solved

IIF function in sql server

Posted on 2004-08-18
7
2,523 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
ID: 11834764
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
ID: 11835461
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
ID: 11837947
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
ID: 11852542
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
ID: 11852578
>>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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

25 Experts available now in Live!

Get 1:1 Help Now