How to list all dates between two dates

Total Post:24


 1543  View(s)
Rate this:
Hi Expert! 

I would like list dates between two date in a SQL Server stored procedure.

For example:

Date1: 2015-05-28
Date2: 2015-05-31

Results :


How to calculate all dates between two given dates?

Thanks in advance. 
  1. Re: How to list all dates between two dates

    Hi Kate!

    Create a stored procedure that does something like the following:

    declare @startDate date;
    declare @endDate date;

    select @startDate = '20150528';
    select @endDate = '20150531';

    with dateRange as
      select dt = dateadd(dd, 1, @startDate)
      where dateadd(dd, 1, @startDate) < @endDate
      union all
      select dateadd(dd, 1, dt)
      from dateRange
      where dateadd(dd, 1, dt) < @endDate
    select *
    from dateRange

    I hope it helpful for you.


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.