This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "A little SQL help with SUM()" Watch "A little SQL help with SUM()" New topic
Author

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Paul,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Braj Prasad
Greenhorn

Joined: Apr 08, 2002
Posts: 16
Try using the following case statement :
select col1, col2,
case
when sum(colName) is NULL then 0
elsesum(colName)
end as Total
from table.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A little SQL help with SUM()
 
Similar Threads
a binary tree question..
question about using boolean to check a condition of an object?
How far we can use Generics?
NullpointerException
Scanner class help