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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:22:29+00:00 2026-05-12T15:22:29+00:00

Consider the need to create a resultset of dates. We’ve got start and end

  • 0

Consider the need to create a resultset of dates. We’ve got start and end dates, and we’d like to generate a list of dates in between.

DECLARE  @Start datetime
         ,@End  datetime
DECLARE @AllDates table
        (@Date datetime)

SELECT @Start = 'Mar 1 2009', @End = 'Aug 1 2009'

--need to fill @AllDates. Trying to avoid looping. 
-- Surely if a better solution exists.

Consider the current implementation with a WHILE loop:

DECLARE @dCounter datetime
SELECT @dCounter = @Start
WHILE @dCounter <= @End
BEGIN
 INSERT INTO @AllDates VALUES (@dCounter)
 SELECT @dCounter=@dCounter+1 
END

How would you create a set of dates that are within a user-defined range using T-SQL?

Assume SQL Server 2005 or later. If your answer is using SQL Server 2008 features, please mark as such.

  • 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-12T15:22:29+00:00Added an answer on May 12, 2026 at 3:22 pm

    If your dates are no more than 2047 days apart:

    declare @dt datetime, @dtEnd datetime
    set @dt = getdate()
    set @dtEnd = dateadd(day, 100, @dt)
    
    select dateadd(day, number, @dt)
    from 
        (select number from master.dbo.spt_values
         where [type] = 'P'
        ) n
    where dateadd(day, number, @dt) < @dtEnd
    

    I updated my answer after several requests to do so. Why?

    The original answer contained the subquery

     select distinct number from master.dbo.spt_values
         where name is null
    

    which delivers the same result, as I tested them on SQL Server 2008, 2012, and 2016.

    However, as I tried to analyze the code that MSSQL internally when querying from spt_values, I found that the SELECT statements always contain the clause WHERE [type]='[magic code]'.

    Therefore I decided that although the query returns the correct result, it delivers the correct result for wrong reasons:

    There may be a future version of SQL Server which defines a different [type] value which also has NULL as values for [name], outside the range of 0-2047, or even non-contiguous, in which case the result would be simply wrong.

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

Sidebar

Related Questions

Consider Microsoft SQL Server 2008 I need to create a table which can be
Consider a student table with 104 rows in it. I need to create groups
I need to create tasks in a SharePoint Task List with c#. Can I
I need to create a table in MS access database. Consider, 'ConfigStructure.mdb' being my
Consider the following DOM manipulation problem: I need to create a span tag after
We're designing a database in which I need to consider some FK(foreign key) constraints.
Over the next few months, we need to consider the best technology/technique for periodically
Consider a situation in which you need to call successive routines and stop as
Consider this page @ http://www.bloodbone.ws/screwed.html I need to be able to have the a.grow
I need to create a large table of contents for an HTML book but

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.