Oracle - 用于获取两个日期时间列之间的分钟差异的最佳 SELECT 语句?

发布于 2024-07-08 02:10:46 字数 223 浏览 6 评论 0原文

我正在尝试满足客户的一个相当困难的报告请求,并且我需要在几分钟内找出两个日期时间列之间的差异。 我尝试将 trunc 和 round 与各种 格式 一起使用,但似乎无法实现找到一个有意义的组合。 有没有一种优雅的方法来做到这一点? 如果没有,有没有任何方法可以做到这一点?

I'm attempting to fulfill a rather difficult reporting request from a client, and I need to find away to get the difference between two DateTime columns in minutes. I've attempted to use trunc and round with various formats and can't seem to come up with a combination that makes sense. Is there an elegant way to do this? If not, is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-07-15 02:10:46
SELECT date1 - date2
  FROM some_table

返回天数差异。 乘以 24 即可得到小时差值,乘以 24*60 即可得到分钟差值。 所以

SELECT (date1 - date2) * 24 * 60 difference_in_minutes
  FROM some_table

应该是你正在寻找的

SELECT date1 - date2
  FROM some_table

returns a difference in days. Multiply by 24 to get a difference in hours and 24*60 to get minutes. So

SELECT (date1 - date2) * 24 * 60 difference_in_minutes
  FROM some_table

should be what you're looking for

記憶穿過時間隧道 2024-07-15 02:10:46

默认情况下,oracle 日期减法返回以天为单位的结果。

因此,只需乘以 24 即可获得小时数,然后再乘以 60 即可获得分钟数。

例子:

select
  round((second_date - first_date) * (60 * 24),2) as time_in_minutes
from
  (
  select
    to_date('01/01/2008 01:30:00 PM','mm/dd/yyyy hh:mi:ss am') as first_date
   ,to_date('01/06/2008 01:35:00 PM','mm/dd/yyyy HH:MI:SS AM') as second_date
  from
    dual
  ) test_data

By default, oracle date subtraction returns a result in # of days.

So just multiply by 24 to get # of hours, and again by 60 for # of minutes.

Example:

select
  round((second_date - first_date) * (60 * 24),2) as time_in_minutes
from
  (
  select
    to_date('01/01/2008 01:30:00 PM','mm/dd/yyyy hh:mi:ss am') as first_date
   ,to_date('01/06/2008 01:35:00 PM','mm/dd/yyyy HH:MI:SS AM') as second_date
  from
    dual
  ) test_data
靖瑶 2024-07-15 02:10:46

where 子句中使用 timestampdiff

例子:

Select * from tavle1,table2 where timestampdiff(mi,col1,col2).

Use timestampdiff at where clause.

Example:

Select * from tavle1,table2 where timestampdiff(mi,col1,col2).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文