Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defaulting to 0 on SUM(some_field) results.

Status
Not open for further replies.

LucL

Programmer
Jan 23, 2006
117
US
Hi Guys,

When I perform some query like.. "SELECT SUM(moneys.m_money) FROM users LEFT JOIN moneys ON moneys.m_user=users.u_id WHERE u_user='test'"

If there are no entries in the MONEYS table for the specific user, I get a blank (NULL) result for the SUM function. However, I would like to get a 0 result.

Is there any way to easily return 0 instead of NULL if the result is null?

Thanks Guys!
Luc L.
 
I should add that I'm actually using this on MAX instead of SUM.
 
use COALESCE
Code:
SELECT [b]coalesce([/b]max(moneys.m_money)[b],0)[/b] 
          as max_money
  FROM users 
LEFT OUTER
  JOIN moneys 
    ON moneys.m_user = users.u_id 
 WHERE u_user = 'test'
:)

r937.com | rudy.ca
 
Did the trick. Thanks a lot man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top