Insert current time minus 1 hour to already inserted date-time records in MYSQL


For subtracting dates, use MySQL DATE_SUB(). Let us first create a −

mysql> create table DemoTable1434
   -> (
   -> ArrivalDatetime datetime
   -> );
Query OK, 0 rows affected (3.14 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1434 values('2019-09-30 21:10:00');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1434 values('2018-09-30 22:20:40');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable1434 values('2017-09-30 23:10:00');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select −

mysql> select * from DemoTable1434;

This will produce the following output −

+---------------------+
| ArrivalDatetime     |
+---------------------+
| 2019-09-30 21:10:00 |
| 2018-09-30 22:20:40 |
| 2017-09-30 23:10:00 |
+---------------------+
3 rows in set (0.00 sec)

Following is the query to insert current time minus 1 hour. We will update here −

mysql> update DemoTable1434
   -> set ArrivalDatetime=date_sub(now(),interval 1 hour);
Query OK, 3 rows affected (0.19 sec)
Rows matched: 3  Changed: 3 Warnings: 0

The current date time is as follows −

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-09-30 21:16:48 |
+---------------------+
1 row in set (0.00 sec)

Let us check the table records once again −

mysql> select * from DemoTable1434;

This will produce the following output −

+---------------------+
| ArrivalDatetime     |
+---------------------+
| 2019-09-30 20:16:39 |
| 2019-09-30 20:16:39 |
| 2019-09-30 20:16:39 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 12-Nov-2019

626 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements