// SQL Server error reference

SQL Server error 8134: Divide by zero error encountered

An expression divided by zero. Guard the divisor with NULLIF so the result is NULL instead of an error.

Error number
8134
Severity
16
Category
Data conversion and truncation
Error message
Divide by zero error encountered.

What it means

Integer and decimal division by zero is an error in SQL Server (with the default ARITHABORT and ANSI_WARNINGS settings), and it stops the statement. It often appears only for some rows, such as a ratio where the denominator is occasionally zero.

Common causes

  • Computing a ratio or percentage where the denominator can be 0.
  • A CASE that doesn't protect every path, or a filter that SQL Server evaluates after the division rather than before.

How to fix it

  1. Turn a zero divisor into NULL with NULLIF.

    SELECT Clicks * 1.0 / NULLIF(Impressions, 0) AS ctr FROM dbo.Ads;
  2. Or decide what the result should be with CASE, or ISNULL(... , 0) around the NULLIF version.

    SELECT ISNULL(Clicks * 1.0 / NULLIF(Impressions, 0), 0) AS ctr FROM dbo.Ads;
  3. Don't rely on WHERE Impressions <> 0 to protect a division in the select list of the same query; SQL Server may evaluate expressions in any order. Put the guard in the expression itself.

Reproduce it safely

SELECT 1 / 0;  -- Msg 8134

Related errors

More data conversion and truncation errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.