PostgreSQL TRIM_SCALE() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL TRIM_SCALE()
function to reduce the value’s scale by removing trailing zeroes.
Introduction to the PostgreSQL TRIM_SCALE() function
The TRIM_SCALE()
function allows you to reduce the scale of a number by removing trailing zeroes.
Note that the scale of a number is a number of fractional decimal digits.
Here’s the syntax of the TRIM_SCALE()
function:
In this syntax, the numeric_value
is a value that you want to trim the scale.
The TRIM_SCALE()
function returns a numeric value with the numeric type after removing trailing zeroes.
It returns NULL
if the numeric_value
is NULL
.
PostgreSQL TRIM_SCALE() function examples
Let’s take some examples of using the TRIM_SCALE()
function.
1) Basic TRIM_SCALE() function example
The following example uses the TRIM_SCALE()
function to reduce the trailing zeroes of the number 123.45000
:
Output:
In this example, the TRIM_SCALE()
function removes the trailing zeroes from the 123.45
000, resulting in 123.45
.
2) Using the TRIM_SCALE() function with table data
We’ll show you an example of using the TRIM_SCALE()
function to standardize the numeric values in a table.
First, create a table called products
to store product data:
Second, insert some rows into the products
table:
Output:
Third, update the prices to remove trailing zeroes using the TRIM_SCALE()
function:
Output:
Summary
- Use the
TRIM_SCALE()
function to reduce the scale of a number scale by removing trailing zeroes.