How can I subtract two dates and give me back hours minutes and seconds Oracle

3

For example if rest 01/01/2017 19:30:00 - 01/01/2017 22:30:30 , in the output should appear 3 horas y 30 segundos

This is what I have tried but it does not return it with the format I want, it returns the values of the date separated in the columns, when trying to put everything together in a column (the commented column) I got this error:

ORA-00904: "FECHA_UNO": identificador no válido
    SELECT FECHA_UNO,
       FECHA_DOS,     
       TRUNC((FECHA_DOS - FECHA_UNO)) DIFERENCIA_DIAS,
       TRUNC((FECHA_DOS - FECHA_UNO) * (24)) DIFERENCIA_HORAS,
       TRUNC((FECHA_DOS - FECHA_UNO) * (60 * 24)) DIFERENCIA_MINUTOS,
       TRUNC((FECHA_DOS - FECHA_UNO) * (60 * 60 * 24)) DIFERENCIA_SEGUNDOS
      -- TRUNC((FECHA_DOS - FECHA_UNO) - (FECHA_DOS - FECHA_UNO (24)) - (FECHA_DOS - FECHA_UNO *(24*60)) -(FECHA_DOS - FECHA_UNO *(24*60*60)) ) DIFERENCIA_TOTAL
  FROM (
SELECT TO_DATE('&1', 'DD.MM.YYYY HH24:MI:SS') FECHA_UNO,
       TO_DATE('&2', 'DD.MM.YYYY HH24:MI:SS') FECHA_DOS
  FROM DUAL
       );
    
asked by campu 26.04.2017 в 13:43
source

3 answers

2

Going back to your original idea:

SELECT 'Dias: ' || TO_CHAR(DIFERENCIA_DIAS, '00') || ' - ' || TO_CHAR(DIFERENCIA_HORAS, '00') || ':' || TO_CHAR(DIFERENCIA_MINUTOS, '00') || ':' || TO_CHAR(DIFERENCIA_SEGUNDOS, '00') 
        AS "Dias: DD - HH24: MI: SS"
FROM (
    SELECT FECHA_UNO,
        FECHA_DOS,
        TRUNC((FECHA_DOS - FECHA_UNO)) DIFERENCIA_DIAS,
        TRUNC(MOD((FECHA_DOS - FECHA_UNO) * 24, 24)) DIFERENCIA_HORAS,
        TRUNC(MOD((FECHA_DOS - FECHA_UNO) * (60 * 24), 60)) DIFERENCIA_MINUTOS,
        TRUNC(MOD((FECHA_DOS - FECHA_UNO) * (60 * 60 * 24), 60)) DIFERENCIA_SEGUNDOS
    FROM (
        SELECT 
            TO_DATE('01/01/2017 19:30:00', 'DD.MM.YYYY HH24:MI:SS') FECHA_UNO,
            TO_DATE('02/01/2017 22:35:09', 'DD.MM.YYYY HH24:MI:SS') FECHA_DOS
        FROM DUAL
        )
    )

Result:

  

Days: 01 - 03: 05: 09

    
answered by 28.04.2017 / 06:26
source
0

You can look at these examples and adapt them to your specific needs.

select
    24 * (sysdate - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
from dual;


select
    24 * (to_date(to_char(sysdate, 'YYYY-MM-DD hh24:mi'), 'YYYY-MM-DD hh24:mi') - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
from dual;

Source: Same question in Stack Overflow in English

    
answered by 26.04.2017 в 14:18
0

This is the solution now I just need to add the seconds, if anyone knows how to add them to modify my answer.

SELECT FECHA_UNO,
   FECHA_DOS,       
TRUNC(ROUND(((FECHA_UNO - FECHA_DOS)*24),3))||':'||
TO_CHAR(TO_DATE(ROUND((ROUND(((FECHA_UNO - FECHA_DOS)*24),3)
-TRUNC(ROUND(((FECHA_UNO - FECHA_DOS)*24),3)))*60),'mi'),'mi') "Diferencia en HORAS y MINUTOS"
FROM (
SELECT TO_DATE('&1', 'DD.MM.YY HH24:MI:SS') FECHA_UNO, -- Formato fecha: 01/01/17 19:45:00
       TO_DATE('&2', 'DD.MM.YY HH24:MI:SS') FECHA_DOS 
  FROM DUAL
       );
    
answered by 27.04.2017 в 09:18