Oracle:如何减去两个日期并获得结果的秒数
Oracle : how to subtract two dates and get seconds of the result
我创建了 oracle NVL function
,它将减去开始和结束时间列并给出结果
但是我没有得到正确的输出值
例子
Start time- 2:33:10
End time -2:33:20
Actual Output - 9 seconds
Expected Output- 10 seconds
查询
NVL(TO_CHAR( table.time1,'YYYY/MM/DD HH24:MI:SS'),' ')"Time1",
NVL(TO_CHAR( table.time2,'YYYY/MM/DD HH24:MI:SS'),' ')"Time2",
NVL(dbms_lob.substr(oprm.message,4000),' ') AS "Messages",
NVL(REGEXP_SUBSTR (CAST( table.time2 AS TIMESTAMP) - CAST( table.time1 AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration
让我看看是否可以帮助阐明您的情况。
首先让我们创建一个 table
-- format the date in my current session.--
alter session set nls_date_format = 'DD-MON-YYYY HH12:MI:SS PM';
CREATE TABLE TIME_HOLDER
(
CREATED_DATE DATE
, SECOND_DATE DATE
)
向其中添加一些记录 table 以开始计算差异。
SET DEFINE OFF;
Insert into TIME_HOLDER (CREATED_DATE,SECOND_DATE) values (to_date('02-NOV-2021 02:01:45 PM','DD-MON-YYYY HH12:MI:SS PM'),to_date('22-NOV-2021 02:01:52 PM','DD-MON-YYYY HH12:MI:SS PM'));
现在要开始使用两个日期列计算两个日期之间的差异,您必须使用类似以下内容的方法。
select extract (day from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ' days - '
|| extract (hour from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ':'
|| extract (minute from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
|| ':'
|| extract (second from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day')) as time_diff
from TIME_HOLDER
首先您可以计算这两个日期之间的间隔,然后从该间隔导出您需要的所有数据:
希望这个例子有助于阐明如何在 oracle 中做这样的事情。
您的数据类型很可能是 TIMESTAMP
,这可以解释舍入问题。
您可以 解决方法 它首先 casting 到 DATE
(以摆脱毫秒)然后将其转换返回 TIMESTAMP
(以便能够执行您的 regexp_substr)
此示例数据重现了您的问题
select opa.*,
NVL(REGEXP_SUBSTR (CAST(opa.end_time AS TIMESTAMP) - CAST(opa.start_time AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration,
NVL(REGEXP_SUBSTR (CAST(CAST(opa.end_time AS DATE)AS TIMESTAMP) - CAST(CAST(opa.start_time AS DATE)AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration2
from tab opa;
START_TIME END_TIME DURATION DURATION2
------------------------------------ ------------------------------------ --------------------------- ---------------------------
04.05.2021 09:13:07,555000000 +02:00 04.05.2021 09:13:18,111000000 +02:00 00:00:10 00:00:11
我创建了 oracle NVL function
,它将减去开始和结束时间列并给出结果
但是我没有得到正确的输出值
例子
Start time- 2:33:10
End time -2:33:20
Actual Output - 9 seconds
Expected Output- 10 seconds
查询
NVL(TO_CHAR( table.time1,'YYYY/MM/DD HH24:MI:SS'),' ')"Time1",
NVL(TO_CHAR( table.time2,'YYYY/MM/DD HH24:MI:SS'),' ')"Time2",
NVL(dbms_lob.substr(oprm.message,4000),' ') AS "Messages",
NVL(REGEXP_SUBSTR (CAST( table.time2 AS TIMESTAMP) - CAST( table.time1 AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration
让我看看是否可以帮助阐明您的情况。 首先让我们创建一个 table
-- format the date in my current session.--
alter session set nls_date_format = 'DD-MON-YYYY HH12:MI:SS PM';
CREATE TABLE TIME_HOLDER
(
CREATED_DATE DATE
, SECOND_DATE DATE
)
向其中添加一些记录 table 以开始计算差异。
SET DEFINE OFF;
Insert into TIME_HOLDER (CREATED_DATE,SECOND_DATE) values (to_date('02-NOV-2021 02:01:45 PM','DD-MON-YYYY HH12:MI:SS PM'),to_date('22-NOV-2021 02:01:52 PM','DD-MON-YYYY HH12:MI:SS PM'));
现在要开始使用两个日期列计算两个日期之间的差异,您必须使用类似以下内容的方法。
select extract (day from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ' days - '
|| extract (hour from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ':'
|| extract (minute from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
|| ':'
|| extract (second from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day')) as time_diff
from TIME_HOLDER
首先您可以计算这两个日期之间的间隔,然后从该间隔导出您需要的所有数据: 希望这个例子有助于阐明如何在 oracle 中做这样的事情。
您的数据类型很可能是 TIMESTAMP
,这可以解释舍入问题。
您可以 解决方法 它首先 casting 到 DATE
(以摆脱毫秒)然后将其转换返回 TIMESTAMP
(以便能够执行您的 regexp_substr)
此示例数据重现了您的问题
select opa.*,
NVL(REGEXP_SUBSTR (CAST(opa.end_time AS TIMESTAMP) - CAST(opa.start_time AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration,
NVL(REGEXP_SUBSTR (CAST(CAST(opa.end_time AS DATE)AS TIMESTAMP) - CAST(CAST(opa.start_time AS DATE)AS TIMESTAMP), '\d{2}:\d{2}:\d{2}'),' ') AS duration2
from tab opa;
START_TIME END_TIME DURATION DURATION2
------------------------------------ ------------------------------------ --------------------------- ---------------------------
04.05.2021 09:13:07,555000000 +02:00 04.05.2021 09:13:18,111000000 +02:00 00:00:10 00:00:11