Update the date and time values while inserting them in MySQL

MySQLMySQLi Database

Here, we will see an example wherein we are inserting datetime and updating them while using INSERT query.

Let us first create a table −

mysql> create table DemoTable816 (DueDate datetime);
Query OK, 0 rows affected (0.45 sec)

Insert some records in the table using insert command. Here is the query to add (minutes / hours / days / months / years) to date when performing INSERT −

mysql> insert into DemoTable816 values(date_add(now(),interval 3 minute));
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable816 values(date_add('2018-01-21 00:00:00',interval 3 Hour));
Query OK, 1 row affected (0.52 sec)
mysql> insert into DemoTable816 values(date_add('2016-11-11 12:40:00',interval 3 Day));
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable816 values(date_add('2018-12-01 11:00:00',interval 3 Month));
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable816 values(date_add('2011-03-21 10:00:00',interval 3 Year));
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable816;

This will produce the following output -

+---------------------+
| DueDate             |
+---------------------+
| 2019-08-03 13:08:43 |
| 2018-01-21 03:00:00 |
| 2016-11-14 12:40:00 |
| 2019-03-01 11:00:00 |
| 2014-03-21 10:00:00 |
+---------------------+
5 rows in set (0.00 sec)
raja
Published on 21-Aug-2019 15:25:41
Advertisements