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

  • Home
  • SEARCH
  • 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 7939983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:15:04+00:00 2026-06-03T23:15:04+00:00

I am trying to query a table that is setup with id, startDateTime, endDateTime.

  • 0

I am trying to query a table that is setup with id, startDateTime, endDateTime. Let’s say row 1 looks like this:

id      startDateTime      endDateTime      
100     2/9/2012 20:55     3/21/2012 10:43

I need to query the above such that I get a distinct count of all the days in the above range.
My expected result would be in the above 42 as there are 42 unique calendar days from 2/9 through 3/21. Datediff because it looks at the time piece gives me 41 days. I have tried various iterations of datediff and timediff trying to get this to work but can’t find anything that works in all scenarios. Any suggestions as to how this can be done in SQL?

I started with a query as shown below:

SELECT ConditionStatus.ID, 
  SUM((DATEDIFF(ConditionStatus.endDate,ConditionStatus.startDate))) AS Duration 
WHERE ID = 100

My query returns a Duration of 41 which is technically accurate but I need to condition such that every date in the range of dates gets a count of 1

I am trying to mimic some logic we use on our datawarehouse where we persist a count of 1 for each date for which there was activity.

Thanks,
Bob

  • 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-06-03T23:15:06+00:00Added an answer on June 3, 2026 at 11:15 pm

    The basic answer is that you can use DATEDIFF() and add 1 because you want to include the current day.

    For example:

    mysql> select datediff(current_date(),current_date()) + 1;
    +---------------------------------------------+
    | datediff(current_date(),current_date()) + 1 |
    +---------------------------------------------+
    |                                           1 |
    +---------------------------------------------+
    1 row in set (0.00 sec)
    

    Expanding on your original example, you can convert the strings to datetimes, then discard the time component, then calculate the inclusive date count with a query like this:

    mysql> SELECT ABS(DATEDIFF(DATE(STR_TO_DATE('2/9/2012 20:55','%m/%d/%Y %H:%i')),
        ->          DATE(STR_TO_DATE('3/21/2012 10:43','%m/%d/%Y %H:%i')))) + 1 as days_in_range;
    +---------------+
    | days_in_range |
    +---------------+
    |            42 |
    +---------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT ABS(DATEDIFF(DATE(STR_TO_DATE('3/21/2012 10:43','%m/%d/%Y %H:%i')),
        ->          DATE(STR_TO_DATE('2/9/2012 20:55','%m/%d/%Y %H:%i')))) + 1 as days_in_range;
    +---------------+
    | days_in_range |
    +---------------+
    |            42 |
    +---------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query that joins another table, but in this
I have a table with itemid|fieldid|value and i'm trying to setup a query that
I'm trying to write a query that updates rows in a table if a
I am trying to generate a query that selects all from a user table
I'm trying to write a query that extracts and transforms data from a table
I'm trying to run an update query that updates one table based on rows
I am trying to write a MySQL query that retrieves one record from table
I am trying to write a query that connects 3 tables. The first table
I'm trying to work out a query that self join itself on a table
Lets say I have a database table called Scrape possibly setup like: UserID (int)

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.