IF…THEN for a SQL SELECT statement

1146060 pts.
Tags:
SQL
SQL statements
How do I do an IF...THEN for a SQL SELECT statement? So I have this already:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
How can I do it?
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    Where is the "THEN" come into play in the statement listed and what should be done if it's true?
    133,675 pointsBadges:
    report
  • carlosdl
    On what DBMS are you trying to do this?

    IF is not a standard SQL function.

    On Oracle, you can use the DECODE function to achieve what you want.  It would be something like this:

    SELECT
      DECODE(Obsolete,'N',1,DECODE(InStock,'Y',1,0),0) AS Saleable,
      p.*
    FROM
      Product p;

    You could also use a CASE construct, which is standard and will work on any DBMS.

    84,805 pointsBadges:
    report
  • carlosdl
    Todd, as I understand it, if Obsolete='N' or InStock='Y', then Saleable will be 1, otherwise it will be 0.
    84,805 pointsBadges:
    report
  • ToddN2000
    Thanks Carlos, the IF was throwing me for a loop as well.
    133,675 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: