如何计算PostgreSQL中的DATE差异?

编程入门 行业动态 更新时间:2024-10-27 06:27:48
本文介绍了如何计算PostgreSQL中的DATE差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

在SQL Server中:就像我们在 SQL Server 中所做的那样,<

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;

问题:

  • 我的方法正确吗?

  • Is my method correct?

PostgreSQL中是否有任何功能来计算这个?

Is there any function in PostgreSQL to calculate this?

推荐答案

您的计算正确表示 DATE 类型,但是如果您的值是时间戳,则应该使用 EXTRACT (或DATE_PART)以确保仅获得整天天;

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

要测试的SQLfiddle 。请注意,时间戳记差异要比整整2天少1秒。

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

更多推荐

如何计算PostgreSQL中的DATE差异?

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

发布评论

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

>www.elefans.com

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