Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the difference between two dates rounded to hours

Tags:

sql

mysql

I'll illustrate what I would like to get in the following example:

'2010-09-01 03:00:00' - '2010-09-01 00:10:00' 

Using TIMEDIFF(), we get 2 as a result. This means, it's not considering the 50 minutes left.

In this case, what I'd like to get is: 50 (minutes) / 60 = 0.83 period. Therefore, the result should be 2.83 and not 2.

like image 863
Brian Roisentul Avatar asked Sep 01 '10 19:09

Brian Roisentul


People also ask

How do you find the difference between two dates in hours?

To calculate the number of hours between two dates we can simply subtract the two values and multiply by 24.

How do I get the difference between two dates and hours in SQL?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

How do I calculate time difference between two dates in Excel?

Another simple technique to calculate the duration between two times in Excel is using the TEXT function: Calculate hours between two times: =TEXT(B2-A2, "h") Return hours and minutes between 2 times: =TEXT(B2-A2, "h:mm") Return hours, minutes and seconds between 2 times: =TEXT(B2-A2, "h:mm:ss")


1 Answers

select time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600;  +-----------------------------------------------------------------------------+ | time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600 | +-----------------------------------------------------------------------------+ |                                                                      2.8333 |  +-----------------------------------------------------------------------------+ 
like image 102
Matthew Avatar answered Oct 08 '22 06:10

Matthew