Solved

IIF function in sql server

Posted on 2004-08-18
7
2,531 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Time out only from one specific computer client 15 52
TSQL remove duplicates from different columns 14 52
Need more granular date groupings 4 44
HIghlights of SSIS? 3 42
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.

732 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