oracle 如何在SQL中计算数值与当前时间的差值?

ru9i0ody  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(108)

我使用旧的设计编号数据类型列来保存到达时间,我使用以下查询将列中的值转换为类似时间格式:

SELECT (TO_CHAR(TO_DATE(LPAD(TO_CHAR(arrival_TIME),4,'0'),'HH24MI'),'HH24:MI')) AS "Arrival time" , 
    TO_NUMBER(TO_CHAR(SYSDATE, 'HH24:MI')) -
    TO_NUMBER((TO_CHAR(TO_DATE(LPAD(TO_CHAR(arrival_TIME),4,'0'),'HH24MI'),'HH24:MI'))) 
     AS "Length of Stay" 
    FROM EMR_FILES 
    WHERE emr_no =00299;

当我运行查询我得到错误

ora-01722 invalid number

如何更正查询并获得到达时间和当前时间之间的差异?

qmelpv7a

qmelpv7a1#

要以INTERVAL DAY TO SECOND数据类型获取时差,可以使用用途:

SELECT LPAD(arrival_TIME,4,'0') AS "Arrival time" , 
       ( TRUNC(SYSDATE, 'MI')
         - TO_DATE(
             TO_CHAR(SYSDATE, 'YYYYMMDD') || LPAD(arrival_TIME,4,'0'),
             'YYYYMMDDHH24MI'
           )
       ) DAY(0) TO SECOND(0) AS "Length of Stay" 
FROM   EMR_FILES 
WHERE  emr_no =00299;

其中,对于样本数据:

CREATE TABLE emr_files (emr_no, arrival_time) AS
SELECT 299,    0 FROM DUAL UNION ALL
SELECT 299, 2359 FROM DUAL;

输出:
| 抵达时间|住院时间|
| --|--|
| 0000 |2019 - 01 - 27 00:00:00|
| 2359 |02:32:00|
fiddle

omqzjyyz

omqzjyyz2#

我从MTO那里得到了这个想法,并通过删除TO_NUMBER和“:”
这样

TO_CHAR(SYSDATE, 'HH24MI') - (TO_CHAR(TO_DATE(LPAD(TO_CHAR(arrival_TIME),4,'0'),'HH24MI'),'HH24MI')) AS "ELOS"

谢谢你的时间

相关问题