Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6033997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:33:56+00:00 2026-05-23T05:33:56+00:00

Given a start date and an end date, I need a count of instances

  • 0

Given a start date and an end date, I need a count of instances between those two dates. So given the following:

Table:

Col 1   Start_Date    End_Date

1       01/01/2010    02/01/2010  
2       01/01/2010    04/01/2010  
3       03/01/2010    04/01/2010  
4       03/01/2010    04/01/2010

If I was looking between the 1st (01/01) and the 2nd (02/01) I would expect a count of 2. If I was looking for the 3rd to the 4th I would expect a count of 3. If I was looking across the whole date range then I would expect a count of 4. Make sense?

NOTE: The dates are already converted to midnight, no code needs to be added for this. Also, dates are in dd/MM/yyyy format throughout this question.

Currently I have something similar to the following:

SELECT COUNT(*), Group_Field
FROM MY_Table
WHERE Start_Date < DATEADD(DAY, 1, @StartDate) AND End_Date > @EndDate
GROUP BY Group_Field

I did at some point think that this was right, but i’m not convinced now…

I did previously have:

WITH Dates AS ( 
            SELECT [Date] = @StartDate
            UNION ALL SELECT [Date] = DATEADD(DAY, 1, [Date])
            FROM Dates WHERE [Date] < @EndDate
) 

SELECT COUNT(*), Group_Field -- In this case it is [Date]
FROM MY_Table
CROSS APPLY Dates
WHERE Start_Date < DATEADD(DAY, 1, @StartDate) AND End_Date > [Date]
GROUP BY Group_Field

But I am not sure that I am using CROSS APPLY properly in this case…

The questions:

1) Am I using Cross Apply right in the 2nd example (and the CTE for that matter)?
2) If so, which logic is right? (I think it’s the 2nd)

/Discuss 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:33:57+00:00Added an answer on May 23, 2026 at 5:33 am

    The solution ended up being:

    WHERE [Date] BETWEEN Start_Date AND DATEADD(Day, -1, End_Date)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two datetime objects; a start date and an end date. I need
I need to generate records on given start-date, end-date and time frequency(Weekly, Monthly etc.)
I need to determine if a given selection is in between a start line
A table exists in Microsoft SQL Server with record ID, Start Date, End Date
How can I get the start and end date of a given year (int),
Given just a std::string iterator, is it possible to determine the start and end
In my script, I need to iterate through a range of dates given the
Using SQL Server 2005 I want to generate the table values between the two
If I was to given a specific java.util.Date, how can I generate a start
Given a DateRange I need to return a list of Start and EndDates that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.