How to Use Trunc() Function In Postgresql?

6 minutes read

The TRUNC() function in PostgreSQL is used to truncate a number to a specified number of decimal places. This function takes two arguments - the number to be truncated and the number of decimal places to retain.


For example, if we have a number 5.6789 and we want to truncate it to 2 decimal places, we would use the TRUNC() function like this:

1
SELECT TRUNC(5.6789, 2);


This would return the value 5.67.


It is important to note that the TRUNC() function does not round the number, it simply removes the decimal places beyond the specified number. If you want to round the number instead of truncating it, you can use the ROUND() function instead.

Best Managed PostgreSQL Providers of November 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
Vultr

Rating is 5 out of 5

Vultr

3
AWS

Rating is 5 out of 5

AWS

4
Cloudways

Rating is 4.9 out of 5

Cloudways


How to truncate a timestamp in PostgreSQL using the trunc() function?

To truncate a timestamp in PostgreSQL using the trunc() function, you can use the following syntax:

1
SELECT trunc(timestamp_column, precision) FROM table_name;


In this syntax:

  • timestamp_column is the name of the column containing the timestamp you want to truncate.
  • precision is the unit to which you want to truncate the timestamp (e.g., 'hour', 'day', 'month', 'year', etc.).


For example, if you want to truncate a timestamp to the nearest hour, you can use the following query:

1
SELECT trunc(timestamp_column, 'hour') FROM table_name;


This will truncate the timestamp to the nearest hour and return the modified timestamp value.


How to apply the trunc() function only to specific columns in a table in PostgreSQL?

To apply the trunc() function only to specific columns in a table in PostgreSQL, you can use a SELECT statement with the trunc() function applied to those specific columns. Here is an example of how you can do this:

1
2
3
4
5
6
SELECT 
  column1,
  column2,
  trunc(column3, 2) as truncated_column3,
  column4
FROM your_table;


In this example, the trunc() function is applied to column3 with 2 decimal places, and the result is aliased as truncated_column3. The SELECT statement returns the original column1, column2, truncated_column3, and column4.


You can modify the trunc() function parameters as needed to adjust the precision or scale of the truncation. Additionally, you can apply the trunc() function to multiple columns in the SELECT statement if desired.


How to apply the trunc() function to an array of numbers in PostgreSQL?

To apply the trunc() function to an array of numbers in PostgreSQL, you can use the UNNEST function along with the ARRAY function to convert the array into a set of rows and then apply the trunc() function to each element in the array. Here is an example:

1
SELECT ARRAY(SELECT trunc(unnest(array[5.678, 8.345, 10.678, 12.345]), 2));


In this example, the trunc() function is applied to each element in the array [5.678, 8.345, 10.678, 12.345] to truncate the decimal values to 2 decimal places. The UNNEST function is used to convert the array into a set of rows, and the ARRAY function is used to convert the result back into an array.


After executing this query, you will get the result as [5.67, 8.34, 10.67, 12.34].


How to use the trunc() function with non-numeric data types in PostgreSQL?

The trunc() function in PostgreSQL is typically used with numeric data types to truncate a number to a specified number of decimal places. However, it can also be used with non-numeric data types as well.


When using the trunc() function with non-numeric data types, you can use it to truncate a string or text value to a specified length. For example, you can use the trunc() function to truncate a string to a specific number of characters.


Here's an example of how you can use the trunc() function with a non-numeric data type in PostgreSQL:

1
SELECT trunc('Hello, World!', 5);


In this example, the trunc() function is used to truncate the string 'Hello, World!' to only include the first 5 characters. The result of this query would be 'Hello'.


You can also use the trunc() function with non-numeric data types to truncate dates or timestamps to a specific unit. For example, you can use the trunc() function to truncate a timestamp to the nearest hour.

1
SELECT trunc('2022-01-01 12:34:56'::timestamp, 'hour');


In this example, the trunc() function is used to truncate the timestamp '2022-01-01 12:34:56' to the nearest hour. The result of this query would be '2022-01-01 12:00:00'.


Overall, the trunc() function in PostgreSQL can be handy for truncating non-numeric data types to a specific length or unit.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

Performing a backup in PostgreSQL using pg_dump is a common method to create a logical backup of your database. Here are the steps involved:Install PostgreSQL: You need to have PostgreSQL installed on your system before performing the backup. Access the Comman...
To integrate Django with PostgreSQL, you need to follow these steps:Install PostgreSQL: Begin by downloading and installing PostgreSQL on your computer. You can find the installation package suitable for your OS on the official PostgreSQL website. Follow the i...
To enforce clients to use SSL for PostgreSQL, you can configure the PostgreSQL server to require SSL connections by setting the ssl parameter to on in the postgresql.conf file. You can also specify that clients must use SSL by setting the sslmode parameter in ...