简体   繁体   English

如何在PostgreSQL中从timestamp(0)舍入毫秒值?

[英]How to round off milliseconds value from timestamp(0) in PostgreSQL?

I need to round off milliseconds value from timestamp(0) without time zone . 我需要将timestamp(0) without time zone毫秒值四舍五入timestamp(0) without time zone

Ex: 2018-04-19 10:43:13. 例:2018-04-19 10:43:13 719 to 2018-04-19 10:43:13. 719至2018-04-19 10:43:13 000 000

您可以使用函数date_trunc

SELECT date_trunc('seconds', '2018-04-19 10:43:13.719'::timestamp);

None of this is applicable to timestamp(0) like you suggested, since that type does not store milliseconds to begin with. 像您建议的那样,这都不适用于timestamp(0) ,因为该类型不存储开始的毫秒数。 Would make sense for literal input or timestamp (without precision modifier). 对于文字输入或timestamp (没有精度修饰符)将是有意义的。

Rounding the timestamp '2018-04-19 10:43:13.719' would give you '2018-04-19 10:43:14' (rounded up ). 将时间戳取整'2018-04-19 10:43:13.719'将得到'2018-04-19 10:43:14'向上取整)。

To truncate microseconds, you can use date_trunc() , like Lorenz already provided. 截断微秒,可以使用date_trunc() ,就像已经提供的Lorenz一样。 According to your comment you still want the redundant dangling '.000'. 根据您的评论,您仍然需要多余的悬空“ .000”。 For display I suggest to_char() : 为了显示我建议to_char()

SELECT to_char(date_trunc('seconds', timestamp '2018-04-19 10:43:13.719'), 'YYYY-MM-DD HH24:MI:SS.MS')

Or cheaper, append '.000' instead of actual milliseconds with to_char() directly: 或更便宜的是,直接使用to_char()附加“ .000”而不是实际的毫秒数:

SELECT to_char(timestamp '2018-04-19 10:43:13.719', 'YYYY-MM-DD HH24:MI:SS".000"')

The manual: 手册:

Ordinary text is allowed in to_char templates and will be output literally. to_char模板中允许使用普通文本,并将按字面输出。 You can put a substring in double quotes to force it to be interpreted as literal text even if it contains template patterns. 您可以将子字符串放在双引号中,以强制将其解释为原义文本,即使它包含模板模式也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM