How to add an offset to all timestamps/DATETIME in a MySQL database?

北城以北 提交于 2019-12-10 21:46:58

问题


I have some MySQL databases that have several tables that contain (amongst others) some DATETIME columns. I am searching for a way to add some amount of time (say one year) to all DATETIME columns in the whole database.

This can be useful if the system-time was wrong when the data was originally written to the database.

OR, as in my case

to create recent DEMO-data for an application out of historical data.

  • Is there a way to shift all DATETIME fields in a Database at once?
  • If not, How can the DATATIME column of all entries in ONE table be shifted (i.e. add some offset)?

thanks for your answers!


回答1:


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

This should do the trick.




回答2:


Try using INTERVAL keyword like

UPDATE table_name SET column_name = column_name + INTERVAL 1 unit

OR possibly this

ADDTIME() adds expr2 to expr1 and returns the result. expr1 is a time or datetime expression, and expr2 is a time expression.

SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');

Unit values goes like this

unit Value  Expected expr Format  
MICROSECOND MICROSECONDS  
SECOND  SECONDS  
MINUTE  MINUTES  
HOUR    HOURS  
DAY         DAYS  
WEEK    WEEKS  
MONTH   MONTHS  
QUARTER QUARTERS  
YEAR    YEARS  


来源:https://stackoverflow.com/questions/9077074/how-to-add-an-offset-to-all-timestamps-datetime-in-a-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!