Skip to Main Content
  • Questions
  • Formula to get exact years of experience of one employee (date delta)

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, pravesh.

Asked: January 05, 2016 - 6:40 pm UTC

Last updated: November 15, 2021 - 4:11 pm UTC

Version: 11gr2

Viewed 10K+ times! This question is

You Asked

Sir,
I want to get exact experience of an employee. Is there any formula to calculate it ?
Because we can't consider each month as 30 days, each year as 365 days. I want to know , how many years, how many months, how many days of experience the particular employee have.
By using MONTHS_BETWEEN we can calculate total no. of months then by dividing it with 12 we can get total no. of years and no.of months. Then how to calculate no. of days out of that ? we can also calculate hours, minutes and seconds out of that particular time , but how?

and Connor said...

Take a look here

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:96012348060

You might need to take care around leap years etc, but this should get you started

SQL> with emp_dates as
  2   ( select to_date('21/04/2011 21:12:12','dd/mm/yyyy hh24:mi:ss') date_from,
  3            to_date('17/09/2015 23:45:19','dd/mm/yyyy hh24:mi:ss') date_to
  4     from dual
  5   )
  6  select
  7    trunc(months_between(date_to,date_from)) total_mths,
  8    date_to - date_from total_days,
  9    (date_to - date_from)*24 total_hours,
 10    (date_to - date_from)*24*60 total_mins,
 11    (date_to - date_from)*24*60*60 total_secs
 12  from emp_dates;

TOTAL_MTHS TOTAL_DAYS TOTAL_HOURS TOTAL_MINS TOTAL_SECS
---------- ---------- ----------- ---------- ----------
        52 1610.10633  38642.5519 2318553.12  139113187

SQL>
SQL>
SQL>
SQL> with emp_dates as
  2   ( select to_date('21/04/2011 21:12:12','dd/mm/yyyy hh24:mi:ss') date_from,
  3            to_date('17/09/2015 23:45:19','dd/mm/yyyy hh24:mi:ss') date_to
  4     from dual
  5   )
  6  select
  7    trunc(months_between(date_to,date_from)/12) years,
  8    mod(trunc(months_between(date_to,date_from)),12) mths,
  9    trunc(date_to - add_months(date_from,trunc(months_between(date_to,date_from)))) days,
 10    trunc(24*mod(date_to - date_from,1)) hrs,
 11    trunc(60*mod(24*mod(date_to - date_from,1),1)) mins,
 12    mod((date_to - date_from)*24*60*60,60) secs
 13  from emp_dates;

     YEARS       MTHS       DAYS        HRS       MINS       SECS
---------- ---------- ---------- ---------- ---------- ----------
         4          4         27          2         33          7


Rating

  (3 ratings)

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

Comments

EXCELLENT

pravesh pattanaik, January 07, 2016 - 6:21 pm UTC

 THANKS A LOT SIR.....Actually sir I want to know, how many years, months, days, hours, minutes, sec the corresponding employee worked for our industry in the form of= "2 years 8 months 3 days 10hours 8 minutes 21 second"

EXCELLENT

pravesh pattanaik, January 07, 2016 - 6:23 pm UTC

THANKS A LOT SIR.....

suresh, November 15, 2021 - 8:57 am UTC

This will works for cloud customers too.
Chris Saxon
November 15, 2021 - 4:11 pm UTC

Yep