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 8914779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:48:43+00:00 2026-06-15T04:48:43+00:00

I have specific date range like From Date To Date ————————— 2012-11-10 2012-11-15 2012-11-21

  • 0

I have specific date range like

From Date        To Date
---------------------------    
2012-11-10       2012-11-15
2012-11-21       2012-11-22
2012-11-30       2012-12-01

I want to write a SQL query which calculates the total no of days between two dates and sum total number of days of particular month

The output I wanted is,

No of days     month
--------------------
   9            11
   1            12

Can anyone help me to write this SQL query?

  • 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-15T04:48:44+00:00Added an answer on June 15, 2026 at 4:48 am

    Ideally, you have a table named “Dates” with all the dates you will ever use, e.g. year 1950 through 2100. This query will give you the result you want:

      select dateadd(m,datediff(m, 0, d.thedate),0) themonth, count(1)
        from dates d
        join ranges r on d.thedate between r.[from date] and r.[to date]
    group by datediff(m, 0, d.thedate)
    order by themonth;
    

    Result:

    |   themonth | COLUMN_1 |
    -------------------------
    | 2012-11-01 |        9 |
    | 2012-12-01 |        1 |
    

    Note that instead of just showing “11” or “12” as month, which doesn’t work well if you have ranges going above 12 months, or doesn’t help sorting when it crosses a new year, this query shows the first day of the month instead.

    If not, you can virtually create a dates table on the fly, per the expanded query below:

    ;with dates(thedate) as (
      select dateadd(yy,years.number,0)+days.number
        from master..spt_values years
        join master..spt_values days
          on days.type='p' and days.number < datepart(dy,dateadd(yy,years.number+1,0)-1)
       where years.type='p' and years.number between 100 and 150
          -- note: 100-150 creates dates in the year range 2000-2050
          --       adjust as required
    )
      select dateadd(m,datediff(m, 0, d.thedate),0) themonth, count(1)
        from dates d
        join ranges r on d.thedate between r.[from date] and r.[to date]
    group by datediff(m, 0, d.thedate)
    order by themonth;
    

    The full working sample is given here: SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create date with specific format for e.g 03-20-2010 have a format
I have been trying to set AlarmManager for Specific date, but it is not
I have a specific requirement to read a resource from jar file and then
I have a specific class C and I would like to overload some math
i have this specific question to do to you, i have a database from
I have a report that has a default date range (Start Date -> End
I have a Rails named_scope which is using a condition to pull specific days
I have some XML which I would like to transform into a report showing
I have some data which I want to retrieve, but I want to have
I have this query: checkins = CheckinAct.objects.filter(time__range=[start, end], location=checkin.location) Which works great for telling

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.