How can I discard/round the millisecond
part, better if the second
part is also removed from a timestamp
w/o timezone
?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Cast it to timestamp(0)
or timestamptz(0)
to remove fractional seconds:
SELECT now()::timestamp(0);
Like @karlgold commented, this rounds to full seconds, and fractions are not stored if you keep operating with this data type.
Use date_trunc()
for more specific needs.
SELECT date_trunc('second', now()::timestamp);
This truncates (leaves seconds unchanged). Further manipulation can re-introduce fractional seconds.
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.