Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql calculating minutes for two dates between two times

I have a table with two DateTimes (StartDateTime, EndDateTime) and I want to calculate the minutes between the two.

Now, I know to use the DATEDIFF() function to get this, but I only want to get the number of minutes that fall between two TIME(0) values.

For example:

  StartDateTime     | EndDateTime         | TimeStart | TimeEnd
2017-06-01 03:00:00 | 2017-06-01 23:00:00 | 06:00:00  | 20:00:00

For the above example, I would get: DATEDIFF(minute, StartDateTime, EndDateTime) = 1200.

However, I want to only get the number of minutes that fall between TimeStart and TimeEnd, which would be 840.

How can I write my query to get this?

like image 398
Nicolas Blaisdell Avatar asked Dec 10 '25 12:12

Nicolas Blaisdell


1 Answers

In SQL Server, DATEDIFF() works on time values, so you can do:

DATEDIFF(minute, TimeStart, TimeEnd)

You can run this as a demonstration:

select DATEDIFF(minute, cast('06:00:00' as time), cast('20:00:00' as time))
like image 67
Gordon Linoff Avatar answered Dec 12 '25 04:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!