Question

I am using MYSQL DB and have a field which contains date value with various formats, eg

1872-03-12
21 SEP 1881
July 14, 1847
1886
08 Apr 1851
10 Feb 1898
2/22/1883
01 Oct 1854
May 9,1909
30 Oct 1856
3 Nov 1856
9 Jan 1856
July 26, 1860
Feb. 16, 1896
1908-1920
24 Nov 1853
1/12/1881
03 Dec 1819
18 Sep 1883
26 Nov 1868

I want to convert it to date format instead of text, also the current date field includes incorrect date values, which I should ignore while converting.

I tried the below but I didn't get good result due to the various formats

Update IGNORE data set pDate = STR_TO_DATE(oldDate,'%d/%c/%Y') where pDate is null;
Was it helpful?

Solution

You have to painstakingly try to figure out the format and then do the conversion:

update ignore data
    set pdate = (case when oldDate rlike '[12][0-9]{3}-[0-1][0-9]-[0-3][0-9]'
                      then str_to_date(oldDate, '%Y-%m-%d')
                      . . .
                 end)

Doesn't sound like fun. But, having inconsistent data in a column is even worse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top