Convert varchar to time

Hi all, I have a 4 character value being stored as varchar in a table that represents time. I need to compare this value using a DATEDIFF to get the difference in minutes to another datetime field however I need to convert it first to time.

The value that I need to convert to time is displayed as 1658 for example. Once converted to time I need to compare it to a time value like 01:59:00.0000000 to get the difference. Any suggestions on converting the varchar value to time?

Thanks!

SELECT CAST(STUFF([4_char_value], 3, 0, ':') AS time)

For example:

SELECT CAST(STUFF([4_char_value], 3, 0, ':') AS time)
FROM ( VALUES('1658') ) AS x([4_char_value])

1 Like

Thanks Scott!