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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:23:07+00:00 2026-05-26T06:23:07+00:00

I need to populate a table that will store the date ranges between 2

  • 0

I need to populate a table that will store the date ranges between 2 given dates: 09/01/11 – 10/10/11

So in this case the table would start from 09/01/11 and store each day till it got to 10/10/11
I was wondering if there was a slick way of doing this in SQL Server – I am currently using SQL Server 2008. Thanks

  • 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-26T06:23:07+00:00Added an answer on May 26, 2026 at 6:23 am

    Easy on SQL 2005+; easier if you have a numbers or tally table. I faked it below:

    DECLARE @StartDate DATE = '20110901'
      , @EndDate DATE = '20111001'
    
    SELECT  DATEADD(DAY, nbr - 1, @StartDate)
    FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS nbr
              FROM      sys.columns c
            ) nbrs
    WHERE   nbr - 1 <= DATEDIFF(DAY, @StartDate, @EndDate)
    

    If you have a tally table, replace the subquery with the table. No recursion.

    EDIT: Since folks seem to have questions about the tally table, let me rewrite this using a zero-based tally table. First, here’s some code to create and populate a table.

    CREATE TABLE [dbo].[nbrs](
        [nbr] [INT] NOT NULL
    ) ON [PRIMARY]
    GO
    
    
    CREATE UNIQUE CLUSTERED INDEX [clidx] ON [dbo].[nbrs]
    (
        [nbr] ASC
    )
    GO
    
    INSERT INTO dbo.nbrs (nbr)
    SELECT nbr-1
    FROM ( SELECT    ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS nbr
              FROM      sys.columns c
            ) nbrs
    GO
    

    Now, that you have the numbers table as a permanent object in your database, you can reuse it for the query INSTEAD of the subquery. The query has also been edited to use a zero-based calculation.

    DECLARE @StartDate DATE = '20110901'
          , @EndDate DATE = '20111001'
    
    SELECT  DATEADD(DAY, nbr, @DateStart)
    FROM    nbrs
    WHERE   nbr <= DATEDIFF(DAY, @DateStart, @DateEnd)
    

    Performant, and no recursion.

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

Sidebar

Related Questions

I need to convert a table redeem to include a date that will be
i need run code that will create a database and populate tables. i am
So I have this SQL table that I need to update. Now we use
I need to add a NOT NULL column to an existing (populated) table that
I have a livesearch script that i need to populate my html menu with
we periodically need to import a CSV that looks like this: Name,SpecID,TestResult1,TestResult2,TestResult3 Alex,ASD123,3.23,452.2,232 Craig,DFG444,453.56,345.3,23
I have a XML file that I need to populate multiple SQL tables, and
First of all, let's define a few tables: Users table will store information about
I need to populate a table with a huge amount of data (many hours
I need to populate a context menu from a database at run time. I

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.