SQL If Statement problem

Posted on 2012-08-15
Last Modified: 2012-08-15
I am using Microsoft SQL Server Management Studio 2008r.  I am trying to create a query that will assign an ABC value based on a cost.  I have no problems doing this in Access, buy SQL is just differenet enough to be a problem.

the component of the select statement is

if(im.cost_avg < 3,'C',if(im.cost_avg > 250,'A','B')) as ABC

I am not sure what I am missing.
Any help will be appreciated.
Question by:Scamquist
    LVL 25

    Expert Comment

    I think SQL Server it is iif - two i's. :
      iif(im.cost_avg < 3,'C',iif(im.cost_avg > 250,'A','B')) as ABC
    LVL 25

    Accepted Solution

    A CASE statement is commonly used instead of iif ... e.g.
      CASE WHEN im.cost_avg < 3 THEN 'C'
                WHEN im.cost_avg > 250 THEN 'A'
                ELSE 'B' END as ABC
    LVL 1

    Author Closing Comment

    Thank you for the assist.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
         When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 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

    23 Experts available now in Live!

    Get 1:1 Help Now