提取小时功能未按预期工作

时间:2013-04-29 10:34:49

标签: sql oracle oracle10g

我有一个选择查询,其中我区分两个时间戳

select EXTRACT (HOUR FROM (systimestamp-queuereceiveddate(a,b))) 
  from dual

queuereceived日期函数也会返回时间戳。我可以通过使用提取功能提取天数,但不能提取小时数。

请告知此事

1 个答案:

答案 0 :(得分:3)

提取小时当然有效:

select systimestamp - (systimestamp - 27.5/24) as diff,
    extract(day from systimestamp - (systimestamp - 27.5/24)) as diff_day,
    extract(hour from systimestamp - (systimestamp - 27.5/24)) as diff_hour
from dual;

DIFF                             DIFF_DAY  DIFF_HOUR
------------------------------ ---------- ----------
+000000001 03:30:00.584929              1          3

如果你希望它显示总小时数,如果差异超过一天,那么你就会误解extract function正在做什么,你会需要通过将day值乘以24并将其与hour值相加来计算总计:

select systimestamp - (systimestamp - 27.5/24) as diff,
    extract(day from systimestamp - (systimestamp - 27.5/24))  * 24
        + extract(hour from systimestamp - (systimestamp - 27.5/24))
        as diff_hour_total
from dual;

DIFF                           DIFF_HOUR_TOTAL
------------------------------ ---------------
+000000001 03:30:00.612830                  27

当然我猜是因为你没有解释你所看到的问题,但由于这个功能确实有效,这是我认为你唯一的意思......