oracle 如何找到两个日期列之间的确切时间差(以分钟为单位)?[副本]

rjzwgtxy  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(148)

此问题已在此处有答案

Calculate difference between 2 date / times in Oracle SQL(21个回答)
5天前关闭。
我有一个名为Temp_cperf的表,其中包含created_date和updated_date列。我想得到这两个日期的时差(以分钟为单位)。
创建日期:16-SEP-23 15:08:14
更新日期:16-SEP-23 15:08:35
我写了这样的查询。

select round((Updated_date - Created_date)*24*60,2) Time_DIFF
from TEMP_CPERF
where C_ID = 12345;

结果是0.35。准确的时间差是0.21。请帮助如何获得准确的时间差分钟。

3gtaxfhh

3gtaxfhh1#

你错过了将结果乘以60一次(秒)。两个日期之差就是它们之间的天数。

SQL> with temp as
  2    (select to_date('16-SEP-23 15:08:35', 'dd-mon-yy hh24:mi:ss') -
  3            to_date('16-SEP-23 15:08:14', 'dd-mon-yy hh24:mi:ss') diff
  4     from dual
  5    )
  6  select diff as days,
  7    diff * 24 as hours,
  8    diff * 24 * 60 as minutes,
  9    diff * 24 * 60 * 60 as seconds
 10  from temp;

      DAYS      HOURS    MINUTES    SECONDS
---------- ---------- ---------- ----------
,000243056 ,005833333        ,35         21

如果你切换到时间戳,事情变得更加明显;两个时间戳的差值是 interval day to second,然后从中提取任何你想要的东西:

SQL> with temp as
  2    (select to_timestamp('16-SEP-23 15:08:35', 'dd-mon-yy hh24:mi:ss') -
  3            to_timestamp('16-SEP-23 15:08:14', 'dd-mon-yy hh24:mi:ss') diff
  4     from dual
  5    )
  6  select diff int_day_to_sec,
  7    extract (hour from diff) as hours,
  8    extract (minute from diff) as minutes,
  9    extract (second from diff) as seconds
 10  from temp;

INT_DAY_TO_SEC                      HOURS    MINUTES    SECONDS
------------------------------ ---------- ---------- ----------
+000000000 00:00:21.000000000           0          0         21

相关问题