I have a varchar field that contains date values like:
02042015
How do I convert it to DATE format that should return the value something like:
2 Apr 2015
I have a varchar field that contains date values like:
02042015
How do I convert it to DATE format that should return the value something like:
2 Apr 2015
You could use CONVERT with 106
format in following:
declare @date varchar(60) = '02042015'
select convert(varchar(20),cast(right(@date,4) + substring(@date,3,2) + left(@date,2) as datetime), 106)
OUTPUT
02 Apr 2015
I thought there would be some conversion format so you could put:
select(convert(datetime,'010109',<some magic number>))
and get the result, but I can't seem to find one that gives the right date
You can try this as well:
declare @dt varchar(6)
select @dt = '010109'
select convert(datetime,RIGHT(@dt,2) + SUBSTRING(@dt,3,2) + LEFT(@dt,2))
Try this
DECLARE @V_DATE VARCHAR(20) = '02042015'
SELECT CONVERT(varchar(11),
CONVERT(datetime,RIGHT(@V_DATE,4)+LEFT(@V_DATE,2)+SUBSTRING(@V_DATE,3,2))
,106)