postgresql 基于秒的 Postregsql 日期差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13626887/
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
Postregsql Date Difference on basis on seconds
提问by Satish Sharma
How can I find date difference between two dates in terms of seconds in PostgreSQL?
如何在 PostgreSQL 中以秒为单位找到两个日期之间的日期差异?
There is no function for giving date difference in terms of seconds like in SQL-Server:
没有像 SQL-Server 那样以秒为单位给出日期差异的功能:
DATE_DIFF(second, '2011-12-30 09:55:56', '2011-12-30 08:54:55')
Please help me to have this in PostgreSQL too
请帮我在 PostgreSQL 中也有这个
回答by araqnid
First, the dates need to be values of timestamp
type (so append ::timestamp
if you're just specifying them as string literals).
首先,日期必须是timestamp
类型值(::timestamp
如果您只是将它们指定为字符串文字,则附加)。
If you subtract two timestamps, the result is of interval
type, which describes a duration of time (in hours, minutes, seconds etc.) You can use extract(epoch from interval_value)
to convert the interval into an absolute number of seconds.
如果减去两个时间戳,则结果是interval
描述持续时间(以小时、分钟、秒等为单位)的类型。您可以使用extract(epoch from interval_value)
将间隔转换为绝对秒数。
So, putting that all together:
所以,把所有这些放在一起:
select extract(epoch from ('2011-12-30 09:55:56'::timestamp - '2011-12-30 08:54:55'::timestamp));
Remember that the ::timestamp
is only needed to convert the string literal to a timestamp: you don't need it if you're using the value of a timestamp column, for example.
请记住,::timestamp
仅需要将字符串文字转换为时间戳:例如,如果您使用时间戳列的值,则不需要它。