- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the difference between dates in the form of months with MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Date1 date, -> Date2 date -> ); Query OK, 0 rows affected (1.04 sec)
Insert some records in the table using insert command &miuns;
mysql> insert into DemoTable values('2017-01-10','2017-12-10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2018-12-31','2015-01-02'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2020-03-01','2019-06-15'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+------------+ | Date1 | Date2 | +------------+------------+ | 2017-01-10 | 2017-12-10 | | 2018-12-31 | 2015-01-02 | | 2020-03-01 | 2019-06-15 | +------------+------------+ 3 rows in set (0.00 sec)
Here is the query to find the difference between dates in the form of months −
mysql> select abs(timestampdiff(MONTH,Date1,Date2)) from DemoTable;
This will produce the following output −
+---------------------------------------+ | abs(timestampdiff(MONTH,Date1,Date2)) | +---------------------------------------+ | 11 | | 47 | | 8 | +---------------------------------------+ 3 rows in set (0.03 sec)
- Related Articles
- Calculate the difference between two dates in days, weeks, months and years in Excel
- Get the difference between dates and calculate salary with MySQL?
- Select dates between current date and 3 months from the current date in MySQL?
- Find the number of logins between two dates in MySQL
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- Find the difference between two timestamps in days with MySQL
- Find the difference between two datetime values with MySQL?
- MySQL query to order and display difference between dates from the current date
- Find the count of users who logged in between specific dates with MongoDB
- Get the SUM of records between two given dates in MySQL
- Select the date records between two dates in MySQL
- How to get the difference between two dates in Android?
- How to calculate the difference between two dates in JavaScript?
- What is the way to find business days between two specified dates in MySQL?
- C# Program to get the difference between two dates

Advertisements