简体   繁体   中英

format table with varchar date format to date mysql

I have a database with multiple tables using date fields.

Some of them have this format as varchar(10) dd-mm-yyyy and other have a mix of dd-mm-yyyy and yyyy-mm-dd .

I tried changing the format of the fields to DATE , however, everything in dd-mm-yyyy turn into 0000-00-00

There is a correct way turn all varchars as date type and only dd-mm-yyyy into DATE format in the tale with mixed date formats before change the field type?

date
--------
01-12-2015 *** change this
05-11-2016 *** this
01-04-2016 ***  and this
2016-05-11 +++ and keep the ones on this format
UPDATE `table` 
SET `Fecha`=date_format(str_to_date(`Datefield`, '%d-%m-%Y'), '%Y-%m-%d') 
WHERE date_format(str_to_date(`Datefield`, '%d-%m-%Y'), '%Y-%m-%d') IS NOT NULL

This worked properly

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