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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:16:52+00:00 2026-05-12T11:16:52+00:00

My question is similar to this MySQL question, but intended for SQL Server: Is

  • 0

My question is similar to this MySQL question, but intended for SQL Server:

Is there a function or a query that will return a list of days between two dates? For example, lets say there is a function called ExplodeDates:

SELECT ExplodeDates('2010-01-01', '2010-01-13');

This would return a single column table with the values:

2010-01-01
2010-01-02
2010-01-03
2010-01-04
2010-01-05
2010-01-06
2010-01-07
2010-01-08
2010-01-09
2010-01-10
2010-01-11
2010-01-12
2010-01-13

I’m thinking that a calendar/numbers table might be able to help me here.


Update

I decided to have a look at the three code answers provided, and the results of the execution – as a % of the total batch – are:

  • Rob Farley’s answer : 18%
  • StingyJack’s answer : 41%
  • KM’s answer : 41%

Lower is better

I have accepted Rob Farley’s answer, as it was the fastest, even though numbers table solutions (used by both KM and StingyJack in their answers) are something of a favourite of mine. Rob Farley’s was two-thirds faster.

Update 2

Alivia’s answer is much more succinct. I have changed the accepted answer.

  • 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-12T11:16:53+00:00Added an answer on May 12, 2026 at 11:16 am

    Try something like this:

    CREATE FUNCTION dbo.ExplodeDates(@startdate datetime, @enddate datetime)
    returns table as
    return (
    with 
     N0 as (SELECT 1 as n UNION ALL SELECT 1)
    ,N1 as (SELECT 1 as n FROM N0 t1, N0 t2)
    ,N2 as (SELECT 1 as n FROM N1 t1, N1 t2)
    ,N3 as (SELECT 1 as n FROM N2 t1, N2 t2)
    ,N4 as (SELECT 1 as n FROM N3 t1, N3 t2)
    ,N5 as (SELECT 1 as n FROM N4 t1, N4 t2)
    ,N6 as (SELECT 1 as n FROM N5 t1, N5 t2)
    ,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)
    SELECT DATEADD(day,num-1,@startdate) as thedate
    FROM nums
    WHERE num <= DATEDIFF(day,@startdate,@enddate) + 1
    );
    

    You then use:

    SELECT *
    FROM dbo.ExplodeDates('20090401','20090531') as d;
    

    Edited (after the acceptance):

    Please note… if you already have a sufficiently large nums table then you should use:

    CREATE FUNCTION dbo.ExplodeDates(@startdate datetime, @enddate datetime)
    returns table as
    return (
    SELECT DATEADD(day,num-1,@startdate) as thedate
    FROM nums
    WHERE num <= DATEDIFF(day,@startdate,@enddate) + 1
    );
    

    And you can create such a table using:

    CREATE TABLE dbo.nums (num int PRIMARY KEY);
    INSERT dbo.nums values (1);
    GO
    INSERT dbo.nums SELECT num + (SELECT COUNT(*) FROM nums) FROM nums
    GO 20
    

    These lines will create a table of numbers containing 1M rows… and far quicker than inserting them one by one.

    You should NOT create your ExplodeDates function using a function that involves BEGIN and END, as the Query Optimizer becomes unable to simplify the query at all.

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

Sidebar

Ask A Question

Stats

  • Questions 245k
  • Answers 245k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Basically, once your random selection of nodes has construct a… May 13, 2026 at 8:10 am
  • Editorial Team
    Editorial Team added an answer You don't manually destroy .Net objects. That's what being a… May 13, 2026 at 8:10 am
  • Editorial Team
    Editorial Team added an answer If your last two steps work, but not using the… May 13, 2026 at 8:10 am

Related Questions

Can you please give me advise? I searched for questions but did not found
I did find other posts similar to this, but wanted a little extra information
In a lot of databases I seem to be working on these days I
A rather complicated SQL query I was working on got me thinking about a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.