I have below sample data:
03202012 as date but the column datatype is Varchar.
I want to convert it to 2012-03-20 00:00:00.000 as Datetime.
I tried using
CAST(CONVERT(CHAR(10), Column, 101) AS DATETIME)
But I get an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Complete code snippet to test:
DECLARE @Column VARCHAR(MAX) = '03202012'
SELECT CAST(CONVERT(CHAR(10), @Column, 101) AS DATETIME)
DATE(if you need the date only - no time portion) or thenDATETIME2(n)for date & time instead ofDATETIME(which has a number of annoying issues).