Convert varchar to date Convert varchar to date database database

Convert varchar to date


The CAST function will do this, the problem is that it will assume you first 2 digits are year. This should for for you:

SELECT CAST((RIGHT('010110',2) + LEFT('010110',4)) AS DATETIME)

This is assuming that all dates are MMDDYY.


Here's a solution

SELECT CAST(SUBSTRING(@date, 5, 2) + SUBSTRING(@date, 1, 4) AS DATETIME)


Get the string into YYMMDD format and you should be in good shape:

declare @x varchar(6)set @x = '091710'declare @d datetimeset @d = cast(RIGHT(@x,2) + LEFT(@x,4) as datetime)select @d