Menu

How to count the number of work days between two dates?

How to count the number of work days between two dates?

Written by Jagdeesh | 2 min read

Problem

To count the number of work days between two dates, taking into account weekends (Saturdays and Sundays) and assuming no holidays.

Input

We will create a table named DateRange with two columns: StartDate and EndDate. Here’s an example input:

sql
CREATE TABLE DateRange (
    StartDate DATE,
    EndDate DATE
);

INSERT INTO DateRange(StartDate, EndDate)
VALUES 
('2023-10-01', '2023-10-10'),
('2023-11-01', '2023-11-15'),
('2023-12-01', '2023-12-31');

ExampleTable:

StartDateEndDate
2023-10-012023-10-10
2023-11-012023-11-15
2023-12-012023-12-31

In this table, we have three date ranges. The goal is to calculate the number of work days for each date range.

Desired Solution

StartDateEndDateWorkDays
2023-10-012023-10-107
2023-11-012023-11-1511
2023-12-012023-12-3122

Solution:

To solve the problem, we need to subtract the weekends between the two dates. Here is the SQL solution to do that:

sql
SELECT 
    StartDate,
    EndDate,
    DATEDIFF(EndDate, StartDate) + 1
    - ((WEEK(EndDate) - WEEK(StartDate)) * 2)
    - CASE WHEN DAYOFWEEK(StartDate) = 1 THEN 1 ELSE 0 END -- Subtract a day if StartDate is a Sunday
    - CASE WHEN DAYOFWEEK(StartDate) = 7 THEN 1 ELSE 0 END -- Subtract a day if StartDate is a Saturday
    - CASE WHEN DAYOFWEEK(EndDate) = 1 THEN 1 ELSE 0 END   -- Subtract a day if EndDate is a Sunday
    - CASE WHEN DAYOFWEEK(EndDate) = 7 THEN 1 ELSE 0 END   -- Subtract a day if EndDate is a Saturday
AS WorkDays
FROM DateRange;

Output:

StartDateEndDateWorkDays
2023-10-012023-10-107
2023-11-012023-11-1511
2023-12-012023-12-3122

Explanation:

This query first calculates the total days between the two dates using DATEDIFF(). It then subtracts the weekends. The WEEK() function returns the week number of a date, and we use it to determine how many full weekends are between the two dates. The CASE statements at the end are for adjusting the count if the StartDate or EndDate is a weekend day.

Note: This solution assumes there are no holidays between the dates. If there are holidays, they will have to be subtracted from the total as well.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to access previous row value in SQL?
  2. How to exclude specific columns using select except?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science