Etienne
Etienne

Reputation: 7201

Convert varchar to time - T-SQL

Please see my code below, I want to convert the SmsStartTime to a time.

So result below needs to be 16:00:00.0000000

DECLARE @SmsStartTime AS VARCHAR(2) = 16

--SELECT @SmsStartTime
SELECT CAST(@SmsStartTime AS TIME) 

And result below needs to be 05:00:00.0000000

DECLARE @SmsStartTime AS VARCHAR(2) = 5

--SELECT @SmsStartTime
SELECT CAST(@SmsStartTime AS TIME) 

Upvotes: 0

Views: 2867

Answers (2)

Krishnraj Rana
Krishnraj Rana

Reputation: 6656

You can try this -

DECLARE @SmsStartTime AS VARCHAR(2) = 16 

SELECT CAST(@SmsStartTime + ':00:00' AS TIME)

Result

16:00:00.0000000

Upvotes: 1

Pawel Czapski
Pawel Czapski

Reputation: 1864

Try this (if you have SQL Server 2012):

DECLARE @SmsStartTime AS VARCHAR(2) = 5
SELECT TIMEFROMPARTS(@SmsStartTime,0,0,0,7)

https://msdn.microsoft.com/en-us/library/hh213398.aspx

Upvotes: 4

Related Questions