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
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
CASEthat doesn't protect every path, or a filter that SQL Server evaluates after the division rather than before.
How to fix it
-
Turn a zero divisor into NULL with
NULLIF.SELECT Clicks * 1.0 / NULLIF(Impressions, 0) AS ctr FROM dbo.Ads;
-
Or decide what the result should be with
CASE, orISNULL(... , 0)around theNULLIFversion.SELECT ISNULL(Clicks * 1.0 / NULLIF(Impressions, 0), 0) AS ctr FROM dbo.Ads;
-
Don't rely on
WHERE Impressions <> 0to 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
8115Arithmetic overflow error converting22012PostgreSQL: division by zeroORA-01476Oracle: divisor is equal to zero
More data conversion and truncation errors
8152String or binary data truncated2628Truncated in table, column245Conversion failed converting a value241Conversion failed for date and/or time242Out-of-range date conversion8114Error converting data type512Subquery returned more than 1 value220Arithmetic overflow error for data type
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.
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.