Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3035
  • Last Modified:

IF condition in a SELECT statement?

Experts,

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)
      BEGIN
      asr.Total_Due as OmniCurrBal
      END
else
      BEGIN
      tr_amount + asr.Total_Due as OmniCurBal
      END


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

0
NigelRocks
Asked:
NigelRocks
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 

or  

SELECT arb.custid,CustomerSub.tr_amount,
ISNULL(tr_amount,0) + asr.Total_Due as OmniCurrBal

Open in new window

0
 
agriffitCommented:
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?
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now