Find the difference between current date and the date records from a MySQL table


To find the difference, use the DATEDIFF() method. Let us first create a table −

mysql> create table DemoTable1446
   -> (
   -> DueDate date
   -> );
Query OK, 0 rows affected (1.42 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1446 values('2019-01-21');
Query OK, 1 row affected (0.69 sec)
mysql> insert into DemoTable1446 values('2019-02-01');
Query OK, 1 row affected (0.44 sec)
mysql> insert into DemoTable1446 values('2019-09-30');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1446;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-01-21 |
| 2019-02-01 |
| 2019-09-30 |
+------------+
3 rows in set (0.10 sec)

The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-10-01 |
+------------+
1 row in set (0.12 sec)

Following is the query get the difference between current date and date records in the table −

mysql> select datediff(curdate(),DueDate) from DemoTable1446;

This will produce the following output −

+-----------------------------+
| datediff(curdate(),DueDate) |
+-----------------------------+
|                         253 |
|                         242 |
|                           1 |
+-----------------------------+
3 rows in set (0.00 sec)

Updated on: 10-Dec-2019

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements