Reputation: 15
I already spent a day looking for the right SQL script just to get the difference of the 2 varchar times.
Here are the varchar columns with each value:
So it will gonna be like this: ProcSchedTime - PtEntersRoomTime = 221 (mins ) I want the result would be 221 in minutes, not 389.
SQL 2005 here.
Im sorry for the confusion.
Please and thank you!
Upvotes: 0
Views: 42
Reputation: 2017
This would grab the fields from a table,
SELECT DATEDIFF(MINUTE, LEFT(PtEntersRoomTime, 2) + ':' + RIGHT(PtEntersRoomTime, 2)
, LEFT(ProcSchedTime, 2) + ':' + RIGHT(ProcSchedTime,2)
) AS [TimeDifference]
FROM TableName
Upvotes: 0
Reputation: 1358
You could make it look like a date time, cast it to a datetime, then use DateDiff
SELECT DATEDIFF(MINUTE,cast(STUFF('1319', 3, 0, ':') as datetime) , cast(STUFF('0930', 3, 0, ':') as datetime))
The STUFF function is just adding a colon in the middle.
Upvotes: 1
Reputation: 11195
To get the difference in mins:
(13 - 09) * 60 + (19 - 30)
select (left(varchartime1,
2
) - left(varchartime2,
2
)
) * 60 + (right(varchartime1,
2
) - right(varchartime2,
2
)
)
Hopefully it won't throw type errors
Upvotes: 0