IF condition in a SELECT statement?

Posted on 2008-11-18
Last Modified: 2012-05-05

I want to put an if condition on a field in my SELECT statement as follows:

SELECT arb.custid,CustomerSub.tr_amount,
if (CustomerSub.tr_amount IS NULL)
      asr.Total_Due as OmniCurrBal
      tr_amount + asr.Total_Due as OmniCurBal

Can this be done?  It's giving me a syntax error on the first "IF" line.

Question by:NigelRocks
    LVL 142

    Accepted Solution

    you mean:
    SELECT arb.custid,CustomerSub.tr_amount,
    CASE WHEN (CustomerSub.tr_amount IS NULL THEN asr.Total_Due ELSE tr_amount + asr.Total_Due END as OmniCurrBal 
    SELECT arb.custid,CustomerSub.tr_amount,
    ISNULL(tr_amount,0) + asr.Total_Due as OmniCurrBal

    Open in new window


    Expert Comment

    How about a set of  extra fields based on a param?

    e.g - ()
    SELECT 1, 2 CASE WHEN param = true THEN 3, 4, 5 ......
    any way to do this?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now