How to update a MySQL date type column?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1451
   -> (
   -> JoiningDate date
   -> );
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1451 values('2019-07-21');
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable1451 values('2018-01-31');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1451 values('2017-06-01');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1451;

This will produce the following output −

+-------------+
| JoiningDate |
+-------------+
| 2019-07-21  |
| 2018-01-31  |
| 2017-06-01  |
+-------------+
3 rows in set (0.00 sec)

Here is the query to update a date type column. We are incrementing the date records with an year −

mysql> update DemoTable1451 set JoiningDate=date_add(JoiningDate, interval 1 year);
Query OK, 3 rows affected (0.15 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1451;

This will produce the following output −

+-------------+
| JoiningDate |
+-------------+
| 2020-07-21  |
| 2019-01-31  |
| 2018-06-01  |
+-------------+
3 rows in set (0.00 sec)
raja
Published on 10-Dec-2019 05:58:07
Advertisements