// math reference

SQL division result scale

Covers how many decimal digits a division produces on each engine, the reason 1.0 / 3 is 0.33333, 0.333333 or 0.33333333333333333333 depending on the database.

Catégorie
Math
Returns
numeric / float
In this category
70 of 82

Syntaxe

1 / 2.0
1.0 / 3
7 / 2

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 Decimal division scale: max(6, s1 + p2 + 1), at least 6 decimal places
PostgreSQL Oui Numeric division selects a scale of at least 16 digits: 1.0::numeric / 3 gives 0.33333333333333333333
MySQL Oui div_precision_increment (default 4) digits added to the dividend's scale: 1 / 3 is 0.3333, 1.0 / 3 is 0.33333
SQLite Not built in No scale concept: non-integer division is IEEE REAL, giving full float digits
Oracle Oui NUMBER division keeps up to 38 significant digits: 1 / 3 is .333333333333333333333333333333333333333

Exemples

MySQL

SELECT 1 / 3 AS int_dividend, 1.0 / 3 AS one_decimal;  -- 0.3333, 0.33333

SQL Server

SELECT CAST(1.0 AS DECIMAL(10, 1)) / 3 AS sql_server_scale;  -- 0.333333

PostgreSQL

SELECT 1.0::numeric / 3 AS pg_scale;  -- 0.33333333333333333333

Bon à savoir

  • On SQL Server, 1.0 / 3 gives 0.333333 (six digits); on PostgreSQL, 0.33333333333333333333 (twenty); on MySQL, 0.33333 (dividend scale 1 plus 4).
  • Rounding happens at the engine's chosen scale, so comparisons against a literal can fail on one engine and pass on another; round both sides explicitly.
  • The scale rule also caps precision: SQL Server decimal division keeps at most 38 total digits, so huge precisions lose decimal places silently.

Questions about division scale

Why does 1.0 / 3 give a different number of digits per engine?

Each engine has a scale policy: SQL Server mandates at least 6 decimal places, MySQL adds div_precision_increment (default 4) to the dividend's scale, PostgreSQL picks at least 16, Oracle computes to 38 digits.

How do I control the scale of a division?

CAST the dividend to the scale you want, and ROUND the result: ROUND(CAST(x AS DECIMAL(18, 4)) / y, 2). Relying on engine defaults invites cross-environment drift.

Why did my decimal division lose precision?

SQL Server caps decimal results at 38 total digits; with very large input precisions, decimal places are the first thing cut. Reduce the operands' declared precision or cast to the needed scale.

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.