PostgreSQL/Math Functions/trunc

Материал из SQL эксперт
Версия от 10:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

trunc(x): Returns the value of x, with any digits past the decimal point truncated

postgres=#
postgres=# -- trunc(x): Returns the value of x, with any digits past the decimal point truncated
postgres=# -- trunc(x, s): Returns the value of x, with any digits past s decimal points truncated
postgres=# SELECT trunc(1.598) AS natural_truncation,
postgres-#        trunc(1.598, 1) AS one_decimal_point,
postgres-#        trunc(1.598, 8) AS extra_places;
 natural_truncation | one_decimal_point | extra_places
--------------------+-------------------+--------------
                  1 |               1.5 |   1.59800000
(1 row)
postgres=#



trunc(x, s): Returns the value of x, with any digits past s decimal points truncated

postgres=#
postgres=# -- trunc(x): Returns the value of x, with any digits past the decimal point truncated
postgres=# -- trunc(x, s): Returns the value of x, with any digits past s decimal points truncated
postgres=# SELECT trunc(1.598) AS natural_truncation,
postgres-#        trunc(1.598, 1) AS one_decimal_point,
postgres-#        trunc(1.598, 8) AS extra_places;
 natural_truncation | one_decimal_point | extra_places
--------------------+-------------------+--------------
                  1 |               1.5 |   1.59800000
(1 row)
postgres=#



Use trunc function in insert statement

postgres=# CREATE TABLE numbers (
postgres(#     number         int
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO numbers VALUES (trunc(99999.99999999, 6));
INSERT 0 1
postgres=#
postgres=# SELECT * FROM numbers;
 number
--------
 100000
(1 row)
postgres=#
postgres=#
postgres=# drop table numbers;
DROP TABLE
postgres=#