简体   繁体   中英

Changing mysql date from an invalid date format

I had date data in excel as dd-mm-yy H:i:s format, I mistakenly imported it to mysql where default format was %Y-%m-%d %H:%i:%s so all my date is wrong now, like 31-01-13 00:00:00 is became 2031-01-13 00:00:00 Now i can't import it again from excel so i need a mysql query to change the date within column.

Thanks Farness

Concatenating substrings from the date should work for all 2000s. For 1900s, you would need an if.

First verify the dates with a SELECT:

SELECT CONCAT(
 '20', 
 substr(date, 9, 2), 
 '-',
 substr(date, 6, 2),
 '-',
 substr(date, 3, 2),
 ' ',
 TIME(date)
)
FROM table

Then you can update the columns in place:

UPDATE table SET date=CONCAT(
 '20', 
 substr(date, 9, 2), 
 '-',
 substr(date, 6, 2),
 '-',
 substr(date, 3, 2),
 ' ',
 TIME(date)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM