PostgreSQL: trunc Function
This PostgreSQL tutorial explains how to use the PostgreSQL trunc function with syntax and examples.
Description
The PostgreSQL trunc function returns a number truncated to a certain number of decimal places.
Syntax
The syntax for the trunc function in PostgreSQL is:
trunc( number, [ decimal_places ] )
Parameters or Arguments
- number
- The number to truncate.
- decimal_places
- Optional. The number of decimal places to truncate to. This value must be a positive or negative integer.
Note
Applies To
The trunc function can be used in the following versions of PostgreSQL:
- PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4
Example
Let's look at some PostgreSQL trunc function examples and explore how to use the trunc function in PostgreSQL.
For example:
postgres=# SELECT trunc(125.315); trunc ------- 125 (1 row) postgres=# SELECT trunc(125.315, 0); trunc ------- 125 (1 row) postgres=# SELECT trunc(125.315, 1); trunc ------- 125.3 (1 row) postgres=# SELECT trunc(125.315, 2); trunc -------- 125.31 (1 row) postgres=# SELECT trunc(125.315, -1); trunc ------- 120 (1 row) postgres=# SELECT trunc(125.315, -2); trunc ------- 100 (1 row) postgres=# SELECT trunc(-125.315); trunc ------- -125 (1 row)
Advertisements