Get last second of a date in MySQL?


To get the last second of a date in MySQL, use the INTERVAL command. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> JoiningDatetime datetime,
   -> DueDatetime datetime
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(JoiningDatetime) values('2019-11-03 12:34:54');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(JoiningDatetime) values('2019-10-04 12:34:54');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+-------------+
| JoiningDatetime     | DueDatetime |
+---------------------+-------------+
| 2019-11-03 12:34:54 | NULL        |
| 2019-10-04 12:34:54 | NULL        |
+---------------------+-------------+
2 rows in set (0.00 sec)

Here is the query to get the last second of a date in MySQL −

mysql> update DemoTable
   -> set DueDatetime=DATE(JoiningDatetime) + INTERVAL 1 DAY - INTERVAL 1 SECOND;
Query OK, 2 rows affected (0.19 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+---------------------+
| JoiningDatetime     | DueDatetime         |
+---------------------+---------------------+
| 2019-11-03 12:34:54 | 2019-11-03 23:59:59 |
| 2019-10-04 12:34:54 | 2019-10-04 23:59:59 |
+---------------------+---------------------+
2 rows in set (0.00 sec)

Updated on: 17-Dec-2019

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements