Link to home
Start Free TrialLog in
Avatar of BobBarker_99
BobBarker_99

asked on

SQL - Adding two select statements, handling NULL

Here is my problem.  

I have a query like: SELECT (SELECT SUM (fld1) FROM tbl1 + SELECT SUM9fld1 FROM tbl2)

This works fine, except when 1 (or both) of the SELECT SUM statements returns NULL.  How can I make it return a 0 instead of a NULL so it doesnt mess up my calculation?

Thanks
Avatar of BobBarker_99
BobBarker_99

ASKER

Just to clarify, the problem is that if the first select returns NULL and the second returns 500
the result is still NULL
SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
select SUM(COALESCE(fldAmount, 0)) FROM tblPaymentOut WHERE fldContactRef = @ContactRef

Is still returning NULL
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Small Change in mathewspatrick's comment:
SELECT (SELECT SUM(COALESCE(fld1,0)) FROM tbl1) 
+ (SELECT SUM(COALESCE(fld1,0)) FROM tbl2) as sum_total

Open in new window