在PG系列衍生版本里,时间戳timestamp通常能够提供最详细的时间表达方式,从年月日到毫秒。
但是在业务开发过程中,我们往往不需要详细到秒小数点后多少位,往往到秒就够了,或者保留小数点后两位。那这种业务需求该如何实现呢,其实很简单,只需要给timestamp一个长度值即可,下面举个例子:
postgres=# select current_timestamp;
now
-------------------------------
2019-01-02 13:50:13.863324+00
(1 row)
postgres=# select current_timestamp::timestamp(2);
now
------------------------
2019-01-02 13:50:26.61
(1 row)
postgres=# select current_timestamp::timestamp(0);
now
---------------------
2019-01-02 13:50:29
(1 row)