Solved

IIF function in sql server

Posted on 2004-08-18
7
2,534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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