-6

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?

Chris
  • 11
  • 1
  • 8

3 Answers3

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
0

This usually works for me:

DATEDIFF(minute, @StarTime, @endTime)
wubblyjuggly
  • 919
  • 3
  • 13
  • 33