Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Update all Dates in a Table

Tags:

mysql

I have a table with 5 million DATETIME records. I want to add a year to all the various datetimes in the table. Can I do it all with a single query? Something like:

SELECT DATE_ADD(*, INTERVAL 1 YEAR);

Or any other way you would recommend. Thanks!

like image 761
Question Overflow Avatar asked Dec 03 '11 04:12

Question Overflow


People also ask

How do you update all records in a table?

Syntax: UPDATE table_name SET column_name1 = new_value1, column_name2 = new_value2 ---- WHERE condition; Here table_name is the name of the table, column_name is the column whose value you want to update, new_value is the updated value, WHERE is used to filter for specific data.

Can update command update all the records of a table?

SQL gives users the option to update existing records in tables with the help of the UPDATE command. Using this command, you can change and alter some (or all) of the records from single or multiple columns of a table.

Can we update the data in view table?

Yes underlying table data can be updated by Updating a view.


1 Answers

This should do what you want:

UPDATE table SET datefield = DATE_ADD(datefield, INTERVAL 1 YEAR);

If you need to update every table in the database check the answers to this question

like image 92
Pastor Bones Avatar answered Sep 30 '22 19:09

Pastor Bones