DBAtlas · Database operations atlas

8134: Divide by zero error encountered

SQL Server

What does it mean?

A divide-by-zero error occurred because the divisor in an arithmetic expression is zero. SQL Server cannot complete the division and stops the query.

Why does it happen?

A column or expression used as the divisor evaluates to 0, a zero denominator in average/ratio calculations, division without NULLIF, or unexpected zero values in the data.

How to fix it

Guard the denominator with NULLIF (e.g. value / NULLIF(denominator, 0)); the result becomes NULL and a default can be supplied with ISNULL/COALESCE. Use CASE WHEN denominator = 0 THEN NULL ELSE value/denominator END. Filter out zero values with WHERE before dividing. If needed, evaluate SET ARITHABORT/ANSI_WARNINGS behavior.

Open in DBAtlas →

More from this engine