简体   繁体   中英

Alter year in a MySQL table

We have a column called date which is type datetime in a MySQL table, in which accidentally rows got added with the wrong year in.

Eg 2015-09-30 23:57:29 instead of 2014-09-30 23:57:29

Is there a quick to change all these dates to the correct year? I was thinking of using date_sub but wasn't sure of the exact syntax?

Try this:

UPDATE yourtable SET field = DATE_SUB(field, INTERVAL 1 YEAR)
WHERE YEAR(field) = 2015

I suppose you want to change all rows with year 2015. If your condition is different, please feel free to change it

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