finn green
finn green

Reputation: 307

nvarchar extract part of string

So I have a varchar holding the duration of an event in 'HHHH:MM:SS' So I was hoping to filter by the hours, so what I would like to do is get only the HHHH, I was thinking of doing a substring(duration, 1, 4) but I'm not sure how to get it to stop at the colon, as we can get less than the four hours, such as '7:07:47' or '29:30:01'. Anything helps thanks guys!

Upvotes: 0

Views: 445

Answers (1)

Lamak
Lamak

Reputation: 70638

You should use the TIME datatype instead. But anyway, here's the code you want:

DECLARE @YourTime VARCHAR(10)
SET @YourTime = '29:30:01'

SELECT LEFT(@YourTime,CHARINDEX(':',@YourTime,0)-1)

Upvotes: 5

Related Questions