1340
MariaDBwarningdatahigh confidence

Division by 0

Production Risk

Medium — silent NULL in non-strict mode can propagate incorrect calculations.

What this means

A division or modulo operation attempted to divide by zero. In strict mode this is an error; otherwise the result is NULL with a warning.

Why it happens
  1. 1Dividing a column value by another column that contains zero
  2. 2Hard-coded divisor of 0 in a query
  3. 3Computed denominator that can evaluate to zero
How to reproduce
trigger — this will error
trigger — this will error
SELECT 10 / 0;

expected output

Warning 1365 (22012): Division by 0  (NULL returned)

Fix 1

Guard with NULLIF

Guard with NULLIF
SELECT 10 / NULLIF(denominator, 0) FROM t;

Why this works

Returns NULL instead of causing an error when the denominator is 0.

Fix 2

Use CASE to handle zero

Use CASE to handle zero
SELECT CASE WHEN denominator = 0 THEN NULL ELSE value / denominator END FROM t;

Why this works

Explicit branch avoids division by zero.

What not to do

Version notes

Sources
Official documentation ↗

MySQL 8.0 — 1365 ER_DIVISION_BY_ZERO (also raised as 1340 context)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors