从时间戳中丢弃毫秒部分 [英] Discard millisecond part from timestamp

查看:533
本文介绍了从时间戳中丢弃毫秒部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果第二部分也从毫秒部分中删除, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆