Skip to Main Content
  • Questions
  • Getting date time stamp difference in hours , minutes & seconds.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Pavan.

Asked: February 28, 2017 - 5:36 pm UTC

Last updated: March 01, 2017 - 12:12 pm UTC

Version: 11g

Viewed 100K+ times! This question is

You Asked

Hi Team,

Could you please help me in understanding how we can find the difference between 2 date time stamp columns of a table to be returned in Hours , minutes & seconds ?

Ex:

I do have a table A with three columns in it with C1 as the primary key and c2 being -intime & c3 - out time

C1

Process1

C2 / Intime

10/7/2013 7:32:50 PM

C3 / Outtime

10/7/2013 7:39:52 PM


I would like to see the result column which displays outtime - intime as 07:02 (i.e 7 mins & 2 seconds ) or the value to bean integer value in seconds - 7*60(sec) + 2 secs = 422 seconds.

The same need to be achieved using an sql query


and Chris said...

If the dates are really timestamps, then this is easy - subtract them and the result is a day to second interval. These are already in hour:minute:second format!

with rws as (
  select timestamp'2017-03-01 01:00:00' t1, 
         timestamp'2017-03-01 02:34:56' t2 
  from dual
)
  select t2-t1  diff_interval
  from rws;

DIFF_INTERVAL        
+00 01:34:56.000000  


Or you can get the components out manually with extract():

with rws as (
  select timestamp'2017-03-01 01:00:00' t1, 
         timestamp'2017-03-01 02:34:56' t2 
  from dual
)
  select extract(hour from (t2-t1)) hrs, 
         extract(minute from (t2-t1)) mins,  
         extract(second from (t2-t1)) secs,
         t2-t1  diff_interval
  from rws;

HRS  MINS  SECS  DIFF_INTERVAL        
1    34    56    +00 01:34:56.000000


If the values are dates, subtracting them yields a number. Extract only works on datetimes or intervals. So you'll get an error:

with rws as (
  select date'2017-03-01' t1, 
         date'2017-03-01'+1/15.1 t2 
  from dual
)
  select extract(hour from (t2-t1)) hrs, 
         extract(minute from (t2-t1)) mins,  
         extract(second from (t2-t1)) secs,
         t2-t1 diff_interval
  from rws;

SQL Error: ORA-30076: invalid extract field for extract source


But you can get around this by casting the dates to timestamps:

with rws as (
  select date'2017-03-01' t1, 
         date'2017-03-01'+1/15.1 t2 
  from dual
), tstamps as (
  select to_timestamp(to_char(t1, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss') t1,
         to_timestamp(to_char(t2, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss') t2
  from rws
)
  select extract(hour from (t2-t1)) hrs, 
         extract(minute from (t2-t1)) mins,  
         extract(second from (t2-t1)) secs,
         t2-t1 diff_interval
  from   tstamps;

HRS  MINS  SECS  DIFF_INTERVAL        
1    35    22    +00 01:35:22.000000  

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

time difference

Rajeshwaran, Jeyabal, March 01, 2017 - 3:45 pm UTC

demo@ORA11G> create table t (x date,y date);

Table created.

demo@ORA11G> insert into t(x,y) values(
  2     to_date('10/7/2013 7:32:50 PM','mm/dd/yyyy hh12:mi:ss am'),
  3     to_date('10/7/2013 7:39:52 PM','mm/dd/yyyy hh12:mi:ss am') );

1 row created.

demo@ORA11G> commit;

Commit complete.

demo@ORA11G> @big_date

Session altered.

demo@ORA11G> 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 y end_date , x start_date from t );

END_DATE                START_DATE                     YRS       MNTS        DYS        HRS       MINS       SECS
----------------------- ----------------------- ---------- ---------- ---------- ---------- ---------- ----------
07-OCT-2013 07:39:52 pm 07-OCT-2013 07:32:50 pm          0          0          0          0          7          2

demo@ORA11G>