Results 1 to 5 of 5

Thread: Divide by Zero

  1. #1
    Join Date
    Aug 2007
    Posts
    3

    Unanswered: 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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    SELECT CASE WHEN COALESCE(SUM(y),0)=0 THEN NULL
    ELSE SUM(x)/SUM(y) END
    FROM ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Is ther an equivalent of SQL Servers NullIf () function in DB2?
    George
    Home | Blog

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    how would you use NULLIF here? are you sure you don't mean ISNULL?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Like this:
    Code:
    SELECT SUM(x) / NULLIF(SUM(Y), 0)
    FROM  ...
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •