SQL isZero() Solution For Divide By Zero Error

I had a query once that contains a divide operation….

SELECT   x, y, ROUND(x/y) as quotient
FROM     dataTbl

It always returns a correct value…
but divide by zero error occurs whenever
there is a zero(0) not NULL value in y column…
now, what i want is instead of having
an error, i want it to return a zero(0) value also…
so I wonder if there is a function isZero(), I can simply
change the y value from 0 to NULL to avoid such error…
I was thinking of a code like this: isZero(y, NULL)…
unfortunately, there is no such functions…
I googled for the correct syntax and I came up with
the: NULLIF(fieldName, condition) function…

SELECT   x, y, ROUND(x/NULLIF(y, 0)) as quotient
FROM     dataTbl

by using the query above, divide by zero error is impossible…
what happen is SQL returns a NULL in y column if its value is zero(0)…
though the error was avoided… the result still does not satisfy me…
it only returns a NULL value in quotient field instead of zero(0)…
well, the solution is simple:

SELECT   x, y, ISNULL(ROUND(x/NULLIF(y, 0)), 0) as quotient
FROM     dataTbl

by using the ISNULL(fieldName, replacementValue) function,
It sort of like reversed the effect of NULLIF function…
what happen in this function is, if the value IS NULL
then replace it with what ever the replacementValue is…
in my query, it replaces the NULL value by zero(0)…

now that’s satisfying…. divide by zero error was avoided
and quotient field is never NULL….

21 Responses to “SQL isZero() Solution For Divide By Zero Error”


  1. 1 Munira

    This is an excellent post!
    Thank you, i was a bit of all the divide by zeros errors i was coming across.
    Thank you!

  2. 2 Diptesh Bose

    This is one of the best thing I found on the internet. Thank you so much..a long awaited project has started moving again…

  3. 3 prosynther

    Thank you so much!!! This was an excellent resource!

  4. 4 Vrujal

    This is an excellent and quick help to keep running my project witout any interruption. thanks alot

  5. 5 Praveen

    This post was excellent and marvellous.

  6. 6 lds1ph

    Great! thanks.

  7. 7 Leigh Tilley

    Excellent helped me solve a production problem very quickly indeed! :-)

  8. 8 Matt

    Fantastic post, clear and concise and solved my problem instantly. Thank you very much, I truly appreciate it!

  9. 9 Andy

    Great work man…Reduced my work by a hell amount time…thanks a lot

  10. 10 Tony

    Worked like a charm. I am using a datagrid to display my data, so I only used the NULLIF function. My datagrid column is set to display zero for Null value. Thanks once again

  11. 11 Tom

    I was about to write my own function to do exactly the same thing as NULLIF until I found this post and realized that one already existed. Thanks!!

  12. 12 Renato

    keep the good work !!!!

  13. 13 Dave

    Doh! Why does it work for everyone else, but I get a “ROUND requires 2 arguments” error?

  14. 14 rs

    double check the arguments on your ROUND function. ROUND(numeric_expression, length). Length is the precision to which the numeric_expression is to be rounded.

  15. 15 B

    Superb , Hiperb , Perfect , Wonderful O La La :)) Thanks …

  16. 16 Steve

    You just saved me, BIG TIME!

    Mucho gracias!

  17. 17 barry bevel

    Would this be the same thing? (db2)

    SELECT X,Y, CASE WHEN Y = 0 THEN 0 ELSE ROUND(X / Y) END AS quotient
    FROM dataTbl

  18. 18 Jeff

    @Dave
    ISNULL(ROUND(x/(NULLIF(y,0)),0),0)

    I’m pretty sure there was an error with the posted code unless they are using something different than sql server where it would work. But with SQL Server the code shown does not work. All three of the functions require more than 1 parameter, so the above code should work fine.

  19. 19 Juraj

    Thanks, this works!

  20. 20 Phani

    Thanks a lot….simplified my problems …..on the spot…..Just Excellent :)

  1. 1 SQL Math Question - Divide By Zero Error | keyongtech

Leave a Reply