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.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL.
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.