I have string date in yymmdd format, for example 150202
I want to convert this string into a valid date in format yyyymmdd, e.g. 20150202.
Thanks in advance.
convert your string to datetime and then do that you want with it
declare @dt varchar(6) = '150213'
select CONVERT(datetime, @dt, 112)
Do another CONVERT to transform it to yyyymmdd format.
SELECT CONVERT(VARCHAR(10), CONVERT(DATETIME, @dt, 112), 112)
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.151302 before the edit. @Ash, thanks!may this will work
select CONVERT(datetime, '150202', 112)
for all date conversions
DATEorDATETIME2(n)don't have any format associated with them - they're stored in binary bytes