Friday, January 11, 2013

Calculate the average between two dates


Question: I am trying to find the average time between two dates, using PLSQL.
For example:
If I wanted the average time between May 1 and May 3, I should get May 2.
Answer: To find the average time between two dates, you could try the following:
select to_date(date1, 'yyyy/mm/dd')
+ ((to_date(date2, 'yyyy/mm/dd') - to_date(date1, 'yyyy/mm/dd')) /2 )
from dual;
This will calculate the elapsed time between date1 and date2. Then it takes half of the elapsed time and adds it to date1. This should give you the average date.
For example, if you wanted to find the average date between May 1 and May 3, you would do the following:
select to_date('2003/05/01', 'yyyy/mm/dd')
+ ((to_date('2003/05/03', 'yyyy/mm/dd') - to_date('2003/05/01', 'yyyy/mm/dd')) / 2)
from dual;