SQL 丢弃时间戳中的毫秒部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10213190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:23:48  来源:igfitidea点击:

Discard millisecond part from timestamp

sqlpostgresqlcastingtimestamprounding

提问by Dipro Sen

How can I discard/round the millisecondpart, better if the secondpart is also removed from a timestampw/o timezone?

millisecond如果second零件也从timestampw/o 中取出,我该如何丢弃/环绕零件timezone

回答by Erwin Brandstetter

A cast to timestamp(0)or timestamptz(0)roundsto full seconds:

转换为timestamp(0)舍入到整秒:timestamptz(0)

SELECT now()::timestamp(0);

Fractions are not stored in table columns of this type.

分数不存储在这种类型的表列中。

date_trunc()truncates(leaves seconds unchanged) - which is often what you really want:

date_trunc()截断(保持秒数不变)——这通常是你真正想要的:

SELECT date_trunc('second', now()::timestamp);

回答by Yuriy Rypka

Discard milliseconds:

丢弃毫秒:

SELECT DATE_TRUNC('second', CURRENT_TIMESTAMP::timestamp);

2019-08-23 16:42:43

2019-08-23 16:42:43

Discard seconds:

丢弃秒数:

SELECT DATE_TRUNC('minute', CURRENT_TIMESTAMP::timestamp);

2019-08-23 16:42:00

2019-08-23 16:42:00

回答by Shriganesh Kolhe

if you just want time, here is the code for postgres

如果你只是想要时间,这里是 postgres 的代码

SELECT DATE_TRUNC('second', CURRENT_TIMESTAMP::timestamp)::time;

it will return 'time without time zone' data type

它将返回“没有时区的时间”数据类型