// math reference

SQL MIN_SCALE function

Returns the minimum number of decimal digits needed to represent a numeric exactly: min_scale(1.2300) is 2.

카테고리
Math
Returns
integer
In this category
17 of 82

구문

MIN_SCALE(numeric)

엔진 지원

엔진Support메모
SQL Server Not built in No equivalent
PostgreSQL 예 PostgreSQL 13+
MySQL Not built in No equivalent; normalize a value by rounding to fewer decimals instead
SQLite Not built in No equivalent; REAL values have no decimal scale
Oracle Not built in No equivalent; NUMBER values do not keep trailing zeros anyway

예시

PostgreSQL

SELECT MIN_SCALE(1.2300::numeric) AS m;  -- 2

PostgreSQL

SELECT MIN_SCALE(amount::numeric) AS needed
FROM rates
ORDER BY needed DESC;

알아 두면 좋은 정보

  • min_scale(1.2300) is 2 but scale(1.2300) is 4: min_scale ignores trailing zeros, scale counts them.
  • For integers the answer is 0: min_scale(42) is 0.
  • Its main use is storage decisions: if min_scale(x) <= 2 (and the integer part fits), the value survives a cast to DECIMAL(10,2) unchanged.

Questions about MIN_SCALE

How is min_scale different from scale?

scale counts every stored digit after the point, trailing zeros included. min_scale returns the minimum needed to be exact: min_scale(1.2300) is 2 where scale is 4.

What is min_scale of an integer?

0. min_scale(42) is 0 because no fractional digits are required.

When would I use it?

Mostly to check whether a value fits a cheaper column: if MIN_SCALE(x) <= 2 the value is exactly representable as DECIMAL(10,2), so casting loses nothing.

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: PostgreSQL.

// 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.