从时间戳中丢弃毫秒部分 [英] Discard millisecond part from timestamp
问题描述
如果第二
部分也从毫秒
部分中删除, code> timestamp w / o timezone
?
How can I discard/round the millisecond
part, better if the second
part is also removed from a timestamp
w/o timezone
?
推荐答案
将其投射到 时间戳(0)
或 timestamptz(0)
删除小数秒:
Cast it to timestamp(0)
or timestamptz(0)
to remove fractional seconds:
SELECT now()::timestamp(0);
像@karlgold注释,这个舍入到整秒,
Like @karlgold commented, this rounds to full seconds, and fractions are not stored if you keep operating with this data type.
使用 date_trunc()
了解更多特定需求。
Use date_trunc()
for more specific needs.
SELECT date_trunc('second', now()::timestamp);
此截断
当然,您可以同时使用这两种方式:删除第二部分 p>
Of course, you can combine both - this time removing the second part as well:
SELECT date_trunc('minute', now())::timestamp(0);
< 分配投放
But be aware that assigning this value to a column or variable of a different data type may result in another assignment cast.
这篇关于从时间戳中丢弃毫秒部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!