MySQL DATE function to return the difference between current date and joining date


At first, find the current date and get the difference between joining date and current date using the DATEDIFF().

The current date is as follows −

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

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> JoiningDate varchar(40)
   -> );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10/10/1998');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('31/12/2010');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('01/01/2017');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('25/10/2019');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output−

+-------------+
| JoiningDate |
+-------------+
|  10/10/1998 |
|  31/12/2010 |
|  01/01/2017 |
|  25/10/2019 |
+-------------+
4 rows in set (0.00 sec)

Here is the query to return the difference between current and joining date −

mysql> select datediff(curdate(),str_to_date(JoiningDate,'%d/%m/%Y')) from DemoTable;

This will produce the following output −

+---------------------------------------------------------+
| datediff(curdate(),str_to_date(JoiningDate,'%d/%m/%Y')) |
+---------------------------------------------------------+
|                                                    7686 |
|                                                    3221 |
|                                                    1028 |
|                                                       1 |
+---------------------------------------------------------+
4 rows in set (0.00 sec)

Updated on: 12-Dec-2019

927 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements