MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?


For this, you can use INTERVAL in MySQL. Let us first create a table −

mysql> create table DemoTable1509
   -> (
   -> ArrivalTime datetime
   -> );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1509 values('2018-01-21 10:20:30');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1509 values('2019-04-01 11:00:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1509 values('2015-12-12 05:45:20');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1509;

This will produce the following output −

+---------------------+
| ArrivalTime         |
+---------------------+
| 2018-01-21 10:20:30 |
| 2019-04-01 11:00:00 |
| 2015-12-12 05:45:20 |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to MySQL update datetime column and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data −

mysql> update DemoTable1509
    -> set ArrivalTime=ArrivalTime+interval 10 year + interval 3 month +interval 22 day+interval 10 hour+interval 30 minute;
Query OK, 3 rows affected (0.17 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1509;

This will produce the following output −

+---------------------+
| ArrivalTime         |
+---------------------+
| 2028-05-13 20:50:30 |
| 2029-07-23 21:30:00 |
| 2026-04-03 16:15:20 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 11-Dec-2019

370 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements