How to avoid the "divide by zero" error in SQL?

Posted by Henrik Staun Poulsen on Stack Overflow See other posts from Stack Overflow or by Henrik Staun Poulsen
Published on 2009-05-14T06:06:05Z Indexed on 2012/06/29 3:16 UTC
Read the original article Hit count: 512

I hate this error message:

Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.

What is the best way to write SQL code, so that I will never see this error message again?

I mean, I could add a where clause so that my divisor is never zero. Or I could add a case statement, so that there is a special treatment for zero. Is the best way to use a NullIf clause?

Is there better way, or how can this be enforced?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server