I have a varchar column where some values are in mm/dd/yyyy format and some are in yyyymmdd.
I want to convert all mm/dd/yyyy dates into the yyyymmdd format. What is the best way to do this? Thanks
Table is Employees and column is DOB
I have a varchar column where some values are in mm/dd/yyyy format and some are in yyyymmdd.
I want to convert all mm/dd/yyyy dates into the yyyymmdd format. What is the best way to do this? Thanks
Table is Employees and column is DOB
On
mm/dd/yyyy corresponds to U.S. standard so if you convert to date using 101 value and then to varchar using 112 for ISO date get the expected result.
declare @table table (date_value varchar(10))
insert into @table values ('03/30/2022'),('20220330')
select date_value
--converted to varchar
,case
--mm/dd/yyyy pattern
when patindex('[0,1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]',date_value)>0 then convert(varchar(10),convert(date,date_value,101),112)
else date_value end date_value_new
--converted to date
,case
when patindex('[0,1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]',date_value)>0 then convert(date,date_value,101)
else convert(date,date_value,112) end date_value_date
from @table
Assuming your "date" column is not actually a date.
or
Returns