File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes A little SQL help with SUM() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "A little SQL help with SUM()" Watch "A little SQL help with SUM()" New topic

A little SQL help with SUM()

Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Hi guys, I have query with a sum() in it, on a DB2 7.0 box.
One of my sum() records returns a null because there are no values to add, but I want it to send back a zero so that it gets printed on a report.
I tired using CASE WHEN to catch the null and print the '0' but I get an erorr, any ideas?
Anurag Gupta
Ranch Hand

Joined: Dec 13, 2001
Posts: 40
In your Query you could use an equivalent of Decode in Oracle, if there is any in DB2.

Anurag Gupta
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33134

How are you getting a null? Are you using rs.getString(column)?
rs.getInt(column) returns 0 if the value is null and the expected value otherwise.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Braj Prasad

Joined: Apr 08, 2002
Posts: 16
Try using the following case statement :
select col1, col2,
when sum(colName) is NULL then 0
end as Total
from table.
I agree. Here's the link:
subject: A little SQL help with SUM()
It's not a secret anymore!