这里我需要计算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 DateDifferenceAn SQLfiddle to test with. Note the timestamp difference being 1 second less than 2 full days.
更多推荐
发布评论