How do I get the "minutes" difference between 2 times in T SQL? I tried DATEDIFF, but it obviously wants dates. This would be the difference in minutes between 2 times in a day. Any suggestions?
Asked
Active
Viewed 1,673 times
-6
-
2`DATEDIFF` works with time values as well – Giorgos Betsos Feb 19 '15 at 17:27
-
2What is the data type of your time data and show some examples. – Rick S Feb 19 '15 at 17:29
-
possible duplicate of [SQL Query DATEDIFF date time fields result in minutes](http://stackoverflow.com/questions/22164002/sql-query-datediff-date-time-fields-result-in-minutes) – AHiggins Aug 19 '15 at 15:54
3 Answers
2
DateDiff
not only work with Date
datatype it even accepts TIME
datatype.
Even if your datatype
is nvarchar
it will work but it should be a valid time
.
Simple Demo
create table time_test(a nvarchar(50),b nvarchar(50))
insert time_test values ('4:00 PM','4:30 PM')
select datediff(minute,a,b) from time_test --30

Pரதீப்
- 91,748
- 19
- 131
- 172
0
I think DATEDIFF would be your best option. I know you are looking for the difference in time within the same day, but couldn't you just pass DATEDIFF two DateTime types with the same date, but different times?
// Should return 300
DATEDIFF(mi, '2015-02-19 08:00:00', '2015-02-19 13:00:00')

Patrick Smith
- 261
- 1
- 6
-
I'm sensing it's the format I have...I have two nvarchar values like "4:00 PM". So - I need to convert. Not sure how? – Chris Feb 19 '15 at 17:40
-
Thank You - I needed to convert the date to 24 hour first. Then this did the trick! Thank You Pat! – Chris Feb 19 '15 at 21:31