SQL TRIM_SCALE function
Reduces a numeric's scale by removing trailing zeros: trim_scale(1.2300) is 1.23 and trim_scale(42.00) is 42.
- Kategorie
- Math
- Returns
numeric- In this category
- 18 of 82
Syntax
TRIM_SCALE(numeric)
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Not built in | No equivalent; FORMAT or string trimming for display |
| PostgreSQL | Ja | PostgreSQL 13+ |
| MySQL | Not built in | No equivalent; DECIMAL always prints its declared scale, and adding 0 does not drop the zeros. Cast to DOUBLE or trim the string for display |
| SQLite | Not built in | No equivalent; REAL already has no decimal-scale concept |
| Oracle | Not built in | NUMBER already drops trailing zeros; there is nothing to trim |
Beispiele
PostgreSQL
SELECT TRIM_SCALE(1.2300::numeric) AS t; -- 1.23
PostgreSQL
SELECT TRIM_SCALE(AVG(discount)::numeric) AS clean_avg FROM orders;
Gut zu wissen
- The result is a numeric with the same value but the smallest scale: trim_scale(1.2300) is 1.23, not 1.2300.
- It changes storage scale, not the value; comparisons and sums are unaffected.
- Only worth having on types that keep trailing zeros. Oracle NUMBER already prints 1.23 for 1.2300; MySQL and SQL Server DECIMAL print the declared scale, 1.2300, and have no trim function.
Questions about TRIM_SCALE
What does trim_scale(42.00) return?
42. All trailing fractional zeros are removed, so the stored scale drops to 0 while the value is unchanged.
How is trim_scale different from round?
round() may change the value to hit a digit count; trim_scale never changes the value, only how many decimal digits are stored.
Why does my DECIMAL still print 1.2300?
MySQL and SQL Server DECIMAL output honours the declared scale regardless of stored zeros. trim_scale is a PostgreSQL numeric feature for stored scale.
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.