I have started work on a existing database where the DOB field has not been setup correctly and is a varchar which has allowed many invalid date formats:
I have been in the process of converting a lot of dates to the format dd/mm/yyyy
This i just a verbose of my queries I have used in the process:
-- Change DateFormat - to /
UPDATE user SET DOB = REPLACE(DOB,'-','/')
WHERE DOB NOT REGEXP
'(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][1-9]|3[0-1])/([1-3][0-9]{3,3})'
AND DOB NOT LIKE '%/%' AND DOB LIKE '%-%'
AND LENGTH(DOB) = 10
-- Change DDMMYYYY DateFormat to DD/MM/YYYY
UPDATE user SET DOB = CONCAT(SUBSTRING(DOB,1,2),'/',SUBSTRING(DOB,3,2),'/',SUBSTRING(DOB,5,4))
WHERE DOB NOT REGEXP
'(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][1-9]|3[0-1])/([1-3][0-9]{3,3})'
AND DOB NOT LIKE '%/%' AND DOB NOT LIKE '%-%'
AND DOB NOT LIKE '%.%' AND DOB NOT LIKE '% %'
AND LENGTH(DOB) = 8
-- Finding Invalid Date Formats
SELECT * FROM user WHERE DOB NOT REGEXP
'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
-- Finding Invalid Date Formats that don't conform to dd/mm/yyyy
SELECT * FROM user
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND LENGTH(DOB) <> 10
AND DOB NOT REGEXP '2?/'
--- converted ddmmyyyy to dd/mm/yyyy
UPDATE user SET DOB = CONCAT(SUBSTRING(DOB,1,2),'/',SUBSTRING(DOB,3,2),'/',SUBSTRING(DOB,5,4))
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND LENGTH(DOB) <> 10
AND DOB NOT REGEXP '2?/'
AND DOB REGEXP '[0-9]{8}'
SELECT * FROM user
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND LENGTH(DOB) <> 10
AND DOB NOT REGEXP '2?[/]'
--- converted ddmmyy to dd/mm/19yy
UPDATE user SET DOB = CONCAT(SUBSTRING(DOB,1,2),'/',SUBSTRING(DOB,3,2),'/','19',SUBSTRING(DOB,5,2))
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND LENGTH(DOB) <> 10
AND DOB NOT REGEXP '2?/'
AND DOB REGEXP '[0-9]{6}'
--- finding dates with alpha's
SELECT * FROM user
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB REGEXP '[A-Z]{1}'
----
removing invalid chars
UPDATE user
SET DOB = REPLACE(DOB,' ',''), DOB =REPLACE(DOB,'.',''), DOB = REPLACE(DOB,',','')
,DOB = REPLACE(DOB,'`',''), DOB =REPLACE(DOB,'//','/'), DOB = REPLACE(DOB,',','')
, DOB = REPLACE(DOB,'O','0')
-- Finding Valid Date Formats
SELECT * FROM user WHERE DOB REGEXP
'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
-- convert dd/mm/yy assuming dob is in the 1900s
UPDATE user SET DOB = CONCAT(SUBSTRING(DOB,1,2),'/',SUBSTRING(DOB,4,2),'/','19',SUBSTRING(DOB,7,2))
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB LIKE '%/%'
AND LENGTH(DOB) = 8
-- remove DOB with . at the end
UPDATE user SET DOB = SUBSTRING(DOB,1,10)
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB LIKE '%.%'
AND DOB NOT LIKE '%/%'
AND DOB LIKE '%.'
--- replacing DOB's with '.' to '/'
UPDATE user SET DOB = REPLACE(DOB,'.','/')
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB LIKE '%.%'
AND DOB NOT LIKE '%/%'
AND DOB REGEXP '^[0-9]'
----
UPDATE user
SET DOB = REPLACE(UPPER(DOB),'JANUARY','01'),
DOB = REPLACE(UPPER(DOB),'FEBURARY','02'),
DOB = REPLACE(UPPER(DOB),'MARCH','03'),
DOB = REPLACE(UPPER(DOB),'APRIL','04'),
DOB = REPLACE(UPPER(DOB),'MAY','05'),
DOB = REPLACE(UPPER(DOB),'JUNE','06'),
DOB = REPLACE(UPPER(DOB),'JULY','07'),
DOB = REPLACE(UPPER(DOB),'AUGUST','08'),
DOB = REPLACE(UPPER(DOB),'SEPTEMBER','09'),
DOB = REPLACE(UPPER(DOB),'OCTOBER','10'),
DOB = REPLACE(UPPER(DOB),'NOVEMBER','11'),
DOB = REPLACE(UPPER(DOB),'DECEMBER','12')
------------
UPDATE user
SET DOB = REPLACE(UPPER(DOB),'JAN','01'),
DOB = REPLACE(UPPER(DOB),'FEB','02'),
DOB = REPLACE(UPPER(DOB),'MAR','03'),
DOB = REPLACE(UPPER(DOB),'APR','04'),
DOB = REPLACE(UPPER(DOB),'MAY','05'),
DOB = REPLACE(UPPER(DOB),'JUN','06'),
DOB = REPLACE(UPPER(DOB),'JUL','07'),
DOB = REPLACE(UPPER(DOB),'AUG','08'),
DOB = REPLACE(UPPER(DOB),'SEP','09'),
DOB = REPLACE(UPPER(DOB),'OCT','10'),
DOB = REPLACE(UPPER(DOB),'NOV','11'),
DOB = REPLACE(UPPER(DOB),'DEC','12')
----------
UPDATE user SET DOB = REPLACE(DOB,'ST','')
,DOB = REPLACE(DOB,'ND','')
,DOB = REPLACE(DOB,'RD','')
,DOB = REPLACE(DOB,'TH','')
------------
SELECT * FROM user
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB NOT REGEXP '2?/'
-------
-- only display dates with 6 numbers ie ddmmyy
select * from user
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND LENGTH(DOB) <> 10
AND DOB NOT REGEXP '2?/'
AND DOB REGEXP '[0-9]{6}'
-- swapping dates from mm/dd/yyyy to dd/mm/yyyy
UPDATE user SET DOB = CONCAT(SUBSTRING(DOB,4,2),'/',SUBSTRING(DOB,1,2),'/',SUBSTRING(DOB,7,4))
WHERE DOB NOT REGEXP '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)'
AND DOB REGEXP '3?[/]'
AND LENGTH(DOB) = 10