Converting string to datetime

Here how you can convert string date to a datetime value:

DECLARE @DateString char(14)
SET @DateString = '20060703074815'
SELECT CAST(SUBSTRING(@DateString, 1, 4) + '-' +
SUBSTRING(@DateString, 5, 2) + '-' +
SUBSTRING(@DateString, 7, 2) + 'T' +
SUBSTRING(@DateString, 9, 2) + ':' +
SUBSTRING(@DateString, 11, 2) + ':' +
SUBSTRING(@DateString, 13, 2) AS datetime)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.