- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update MySQL date and increment by one Year?
You can use in-built function date_add() from MySQL. The syntax is as follows −
UPDATE yourTableName SET yourDateColumnName=DATE_ADD(yourDateColumnName,interval 1 year);
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table UpdateDate -> ( -> Id int, -> DueDate datetime -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command. The query to insert record is as follows −
mysql> insert into UpdateDate values(1001,'2012-5-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into UpdateDate values(1002,'2013-8-2'); Query OK, 1 row affected (0.17 sec) mysql> insert into UpdateDate values(1003,'2014-2-27'); Query OK, 1 row affected (0.10 sec) mysql> insert into UpdateDate values(1004,'2016-11-1'); Query OK, 1 row affected (0.18 sec) mysql> insert into UpdateDate values(1005,'2017-12-24'); Query OK, 1 row affected (0.12 sec)
Let us now display all records from the table using select command. The query is as follows −
mysql> select *from UpdateDate;
Output
+------+---------------------+ | Id | DueDate | +------+---------------------+ | 1001 | 2012-05-21 00:00:00 | | 1002 | 2013-08-02 00:00:00 | | 1003 | 2014-02-27 00:00:00 | | 1004 | 2016-11-01 00:00:00 | | 1005 | 2017-12-24 00:00:00 | +------+---------------------+ 5 rows in set (0.00 sec)
Here is the query that can be used to update date with 1 year using date_add() and interval. The query is as follows −
mysql> update UpdateDate set DueDate=date_add(DueDate,interval 1 year); Query OK, 5 rows affected (0.15 sec) Rows matched: 5 Changed: 5 Warnings: 0
Check the table using select command. The query is as follows −
mysql> select *from UpdateDate;
Output
+------+---------------------+ | Id | DueDate | +------+---------------------+ | 1001 | 2013-05-21 00:00:00 | | 1002 | 2014-08-02 00:00:00 | | 1003 | 2015-02-27 00:00:00 | | 1004 | 2017-11-01 00:00:00 | | 1005 | 2018-12-24 00:00:00 | +------+---------------------+ 5 rows in set (0.00 sec)
Look at the above sample output, all values have been updated. If you want to update a particular date time then use the where condition.
Advertisements
To Continue Learning Please Login
Login with Google