Like Chirs, said - Care should be taken for julian date preparation,
demo@ORA12C> select to_date('31/03/2016 11:19:58', 'dd/mm/yyyy hh24:mi:ss') end_date ,
2 to_date('01/01/2016 10:18:57', 'dd/mm/yyyy hh24:mi:ss') start_date
3 from dual ;
END_DATE START_DATE
-------------------- --------------------
31-MAR-2016 11:19:58 01-JAN-2016 10:18:57
1 row selected.
demo@ORA12C> select end_date,start_date,
2 trunc(months_between(end_date,start_date)/12) as yrs ,
3 trunc(mod( months_between(end_date,start_date) ,12)) as mnts,
4 trunc(end_date - add_months( start_date, months_between(end_date,start_date))) as dys,
5 trunc(24*mod(end_date - start_date,1)) as hrs,
6 trunc( mod(mod(end_date - start_date,1)*24,1)*60 ) as mins ,
7 mod(mod(mod(end_date - start_date,1)*24,1)*60,1)*60 as secs
8 from ( select to_date('31/03/2016 11:19:58', 'dd/mm/yyyy hh24:mi:ss') end_date ,
9 to_date('01/01/2016 10:18:57', 'dd/mm/yyyy hh24:mi:ss') start_date
10 from dual );
END_DATE START_DATE YRS MNTS DYS HRS MINS SECS
-------------------- -------------------- ---------- ---------- ---------- ---------- ---------- ----------
31-MAR-2016 11:19:58 01-JAN-2016 10:18:57 0 2 30 1 1 1
1 row selected.
demo@ORA12C> select to_date( to_char(yrs,'fm0000') ||
2 to_char(mnts,'fm00') ||
3 to_char(dys,'fm00') ||
4 to_char(hrs,'fm00') ||
5 to_char(mins,'fm00') ||
6 to_char(secs,'fm00') ,'YYYYMMDDHH24MISS')
7 from (
8 select end_date,start_date,
9 trunc(months_between(end_date,start_date)/12) as yrs ,
10 trunc(mod( months_between(end_date,start_date) ,12)) as mnts,
11 trunc(end_date - add_months( start_date, months_between(end_date,start_date))) as dys,
12 trunc(24*mod(end_date - start_date,1)) as hrs,
13 trunc( mod(mod(end_date - start_date,1)*24,1)*60 ) as mins ,
14 mod(mod(mod(end_date - start_date,1)*24,1)*60,1)*60 as secs
15 from ( select to_date('31/03/2016 11:19:58', 'dd/mm/yyyy hh24:mi:ss') end_date ,
16 to_date('01/01/2016 10:18:57', 'dd/mm/yyyy hh24:mi:ss') start_date
17 from dual )
18 );
to_char(mins,'fm00') ||
*
ERROR at line 5:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
demo@ORA12C>
So Given two dates as inputs, stick with getting the difference in terms of Years, Months, Days, Hours, Mins, Sec and dont put every thing back into a Date format - that may not work for all cases ( once such example is above )