How to get the difference between date records and the current date in MySQL?

MySQLMySQLi Database

Let’s say the current date is 2019-09-06. For our example, we will first create a table −

mysql> create table DemoTable
(
   AdmissionDate date
);
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-08');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('2018-09-06');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('2016-10-26');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-01-08    |
| 2018-09-06    |
| 2016-10-26    |
+---------------+
3 rows in set (0.00 sec)

Following is the query to get the difference between date records and current date;

mysql> select datediff(now(),AdmissionDate) from DemoTable;

This will produce the following output −

+-------------------------------+
| datediff(now(),AdmissionDate) |
+-------------------------------+
| 241                           |
| 365                           |
| 1045                          |
+-------------------------------+
3 rows in set (0.00 sec)
raja
Published on 07-Oct-2019 14:45:59
Advertisements