PostgreSQL:如何计算PostgreSQL中的DATE差异?(PostgreSQL: How to calculate DATE Difference in PostgreSQL?)

编程入门 行业动态 更新时间:2024-10-28 16:30:56
PostgreSQL:如何计算PostgreSQL中的DATE差异?(PostgreSQL: How to calculate DATE Difference in PostgreSQL?)

这里我需要计算PostgreSQL中两个日期的差异。

在SQL Server中 :就像我们在SQL Server中执行操作一样简单。

DATEDIFF(Day, MIN(joindate), MAX(joindate)) AS DateDifference;

我的尝试 :我正在尝试使用以下脚本:

(Max(joindate) - Min(joindate)) as DateDifference;

问题

我的方法是正确的吗?

PostgreSQL中有没有函数来计算这个值?

Here I need to calculate the difference of the two dates in the PostgreSQL.

In SQL Server: Like we do in SQL Server its much easier.

DATEDIFF(Day, MIN(joindate), MAX(joindate)) AS DateDifference;

My Try: I am trying using the following script:

(Max(joindate) - Min(joindate)) as DateDifference;

Question:

Is my method is correct?

Is there any function in PostgreSQL to calculate this?

最满意答案

您的计算对于DATE类型是正确的,但是如果您的值是时间戳,则应该使用EXTRACT (或DATE_PART)确保只获取完整日期的差异;

EXTRACT(DAY FROM MAX(joindate)-MIN(joindate)) AS DateDifference

用于测试的SQLfiddle 。 请注意,时间戳差异比整整两天少1秒。

Your calculation is correct for DATE types, but if your values are timestamps, you should probably use EXTRACT (or DATE_PART) to be sure to get only the difference in full days;

EXTRACT(DAY FROM MAX(joindate)-MIN(joindate)) AS DateDifference

An SQLfiddle to test with. Note the timestamp difference being 1 second less than 2 full days.

更多推荐

本文发布于:2023-08-04 19:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:差异   DATE   PostgreSQL   calculate   Difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!