If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Divide by Zero

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-16-07, 12:14
d000hg d000hg is offline
Registered User
 
Join Date: Aug 2007
Posts: 3
Divide by Zero

Say you have SELECT SUM(x)/SUM(y) FROM ...

We're using a DB2 DB from Java and the exception we see is not useful for knowing that a divide-by-0 has happended, and we want to know this. What can we do to a)trap the problem and b)signal it?

Thanks a lot.
Reply With Quote
  #2 (permalink)  
Old 10-16-07, 12:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
SELECT CASE WHEN COALESCE(SUM(y),0)=0 THEN NULL
ELSE SUM(x)/SUM(y) END
FROM ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-16-07, 13:04
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Is ther an equivalent of SQL Servers NullIf () function in DB2?
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 10-16-07, 13:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
how would you use NULLIF here? are you sure you don't mean ISNULL?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-16-07, 17:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Like this:
Code:
SELECT SUM(x) / NULLIF(SUM(Y), 0)
FROM  ...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On