
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to count days in date range with start and end date
To count days in date range, you need to find the difference between dates using DATEDIFF().
Let us first create a table:
mysql> create table DemoTable730 ( StartDate date, EndDate date ); Query OK, 0 rows affected (0.45 sec)
Insert some records in the table using insert command:
mysql> insert into DemoTable730 values('2019-01-21','2019-07-21'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable730 values('2018-10-11','2018-12-31'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable730 values('2016-01-01','2016-12-31'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement:
mysql> select *from DemoTable730;
This will produce the following output -
+------------+------------+ | StartDate | EndDate | +------------+------------+ | 2019-01-21 | 2019-07-21 | | 2018-10-11 | 2018-12-31 | | 2016-01-01 | 2016-12-31 | +------------+------------+ 3 rows in set (0.00 sec)
Following is the query to count days in date range:
mysql> select ABS(DATEDIFF(StartDate,EndDate)) AS Days from DemoTable730;
This will produce the following output -
+------+ | Days | +------+ | 181 | | 81 | | 365 | +------+ 3 rows in set (0.00 sec)
- Related Articles
- Ignoring the year in MySQL Query with date range?
- How to calculate end date from start date and duration in Excel?
- Write a Python function to calculate the total number of business days from a range of start and end date
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- MySQL query to delete a DATE older than 30 days from another date?
- MySQL query to fetch date more recent than 14 days?
- Add 30 days to date in a MySQL table with arrival date records
- Check if the current date falls in a given date range using MySQL query
- How to calculate quarter start date or end date based on a given date in Excel?
- How to add a year and two days to a date with a single MySQL query?
- MySQL query to insert row with date?
- MySQL query to fetch date with year and month?
- MySQL add days to a date?
- Get timestamp date range with MySQL Select?
- MySQL query to find expiry date (record) from the next 2 days?

Advertisements