// math reference

SQL decimal overflow

Describes what happens when a value exceeds a decimal column's declared precision or scale: errors versus silent rounding, per engine.

Catégorie
Math
Returns
numeric
In this category
64 of 82

Syntaxe

NUMERIC(5, 2)
INSERT ... VALUES (1234.567)

Each block is a separate form. The support table and the examples show which engine takes which.

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Oui Error: 'Arithmetic overflow error converting numeric to data type numeric' when p is exceeded; scale overflow rounds on storage
PostgreSQL Oui Error 'numeric field overflow' when integer digits exceed p - s; excess scale digits round to the declared scale
MySQL Oui Excess scale rounds; excess integer digits clamp to the largest value (CAST(12345.6 AS DECIMAL(5,2)) is 999.99 with a warning), and an INSERT in strict mode fails instead
SQLite Not built in No decimal type: NUMERIC affinity stores REAL or INTEGER, so there is nothing to overflow
Oracle Oui NUMBER(p, s) raises ORA-01438 when the value is larger than the precision allows

Exemples

SQL Server

SELECT CAST(1234.567 AS NUMERIC(6, 2)) AS fits;  -- 1234.57
SELECT CAST(1234.567 AS NUMERIC(5, 2)) AS overflow;  -- Msg 8115, arithmetic overflow

MySQL

SELECT CAST(1234.567 AS DECIMAL(6, 2)) AS rounded, CAST(12345.6 AS DECIMAL(5, 2)) AS clamped;  -- 1234.57, 999.99

PostgreSQL

SELECT 12345.6::NUMERIC(5, 2) AS boom;  -- ERROR: numeric field overflow

Bon à savoir

  • Two different failures hide here: integer-digit overflow (too many digits before the point, an error everywhere except MySQL's clamp) and scale overflow (too many after, rounded on every engine).
  • PostgreSQL's unsuffixed numeric never overflows; precision limits apply only when you declare NUMERIC(p, s).
  • Arithmetic widens the result type rather than overflowing: on SQL Server NUMERIC(5,2) * NUMERIC(5,2) is NUMERIC(11,4), so 999.99 * 999.99 = 999980.0001 fits. The overflow comes when you store that into a NUMERIC(5,2) column.

Questions about DECIMAL overflow

What is the difference between precision and scale overflow?

Precision is total digits, scale is digits after the point. Too many integer digits errors on SQL Server, PostgreSQL and Oracle and clamps (CAST) or errors (strict INSERT) on MySQL. Too many decimals just rounds on every engine.

Does plain NUMERIC ever overflow in PostgreSQL?

No. NUMERIC without (p, s) grows as needed. The 'numeric field overflow' error appears only for declared NUMERIC(p, s) columns and casts.

Why does MySQL round instead of erroring?

MySQL's traditional behaviour is to fit the value to the type: round the scale and clamp the integer part, with a warning. In the default strict mode an INSERT or UPDATE that would clamp fails instead; CAST in a SELECT still clamps.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.