Convert string with different format to date

后端 未结 2 1817
攒了一身酷
攒了一身酷 2021-01-06 14:46

I have a table with a string column (varchar(200)) that contain a date in different format. For example

may 24 1983 12:00AM
1981-01-13 00:00:00
1979         


        
2条回答
  •  天命终不由人
    2021-01-06 15:34

    I would try to do this using a server side language, perhaps PHP and use strotime to generate a pretty date.

    Otherwise, the only other option I see is to start writing CASEs to read possible formats, and output using DATE_FORMAT. Perhaps something like:

    Pseudo-code (for case may 24 1983 12:00AM)

    CASE
        WHEN col (after first space is some digit) 
            AND (after second space is 4 digits)
            AND (after third space has 'AM' or 'PM')
        THEN
            DATE_FORMAT(col, '%....')
    

    You can also trying using REGEXP to for pattern matching.

提交回复
热议问题