
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Days Between Two Dates in MySQL
Let us first create a table −
mysql> create table DemoTable1471 -> ( -> EmployeeJoiningDate date, -> EmployeeRelievingDate date -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1471 values('2018-06-21','2018-12-21'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1471 values('2017-01-19','2019-01-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1471 values('2015-12-31','2016-03-01'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1471;
This will produce the following output −
+---------------------+-----------------------+ | EmployeeJoiningDate | EmployeeRelievingDate | +---------------------+-----------------------+ | 2018-06-21 | 2018-12-21 | | 2017-01-19 | 2019-01-31 | | 2015-12-31 | 2016-03-01 | +---------------------+-----------------------+ 3 rows in set (0.00 sec)
Here is the query to calculate the days between two dates −
mysql> select datediff(EmployeeRelievingDate,EmployeeJoiningDate) as EmployeeWorkDays from DemoTable1471;
This will produce the following output −
+------------------+ | EmployeeWorkDays | +------------------+ | 183 | | 742 | | 61 | +------------------+ 3 rows in set (0.00 sec)
Advertisements