我正在计算两个时间戳之间的差异,当差异超过48小时时,这两个时间戳在某种程度上不起作用。
这是我的代码:
select systimestamp, mystoredtimestamp,
extract( hour from diff ) || ' hours ' || extract( minute from diff ) || ' minutes'
from (select (systimestamp - mystoredtimestamp)) diff from dual)
输出
1. systimestamp: 30-OCT-17 11.48.43.783572 AM +00:00,
2. mystoredtimestamp: 27-OCT-17 07.30.36.956687 AM,
3. difference: 4 hours 18 minutes
(不知何故,此降价编号未呈现,以上是查询的三个不同列)
mystoredtimestamp是TIMESTAMP(6) WITH LOCAL TIME ZONE
数据类型。这是什么导致了这个问题?如果是这样,我现在如何解决这个问题?
答案 0 :(得分:1)
您还需要提取日期。它不会自动转换为小时数。
SELECT
extract ( DAY FROM diff )
|| ' days '
||extract( hour FROM diff )
|| ' hours '
|| extract( minute FROM diff )
|| ' minutes'
FROM
(
SELECT
systimestamp - CAST ( '27-OCT-17 07.30.36.956687 AM' AS TIMESTAMP(6)
WITH LOCAL TIME ZONE ) diff
FROM
DUAL
);
<强> O / P 强>
3 days 2 hours 26 minutes
如果您想要在几小时内,乘以24并添加天数。
SELECT
24 * extract ( DAY FROM diff )
+
extract( hour FROM diff )
|| ' hours '
|| extract( minute FROM diff )
|| ' minutes'
FROM
(
SELECT
systimestamp - CAST ( '27-OCT-17 07.30.36.956687 AM' AS TIMESTAMP(6)
WITH LOCAL TIME ZONE ) diff
FROM
DUAL
);
<强> O / P 强>
74 hours 28 minutes