地板时间戳到秒

时间:2012-02-22 02:56:55

标签: sql postgresql

select current_timestamp(6),    current_timestamp(0)
----------------------------------------------------
2012-02-22 16:03:45.988418+13 | 2012-02-22 16:03:46+13
           -------^                   ------------^ (date from the future)

由于current_timestamp(0)以“数学方式”舍入,此查询可以轻松返回第二列的未来时间戳。这可能会导致一些意外错误,因为检索到的时间是从未来到0.5秒。

所以我的问题是 - 将current_timestamp(6)舍入到最接近的第二位的最简单方法是什么?

PS:我知道转换为秒并返回的解决方案

PPS:结果应该是时间戳类型,而不是字符串

UPD

找到解决方案

select current_timestamp(6), current_timestamp(0)::abstime

1 个答案:

答案 0 :(得分:3)

select current_timestamp(6),    date_trunc('second', current_timestamp(6))
相关问题