Home > DeveloperSection > Forums > How to list all dates between two dates
Kate Smith
Kate Smith

Total Post:24

Points:168
Posted on    July-08-2013 8:52 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 818  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 :

2015-05-29
2015-05-30

How to calculate all dates between two given dates?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    July-08-2013 9:02 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick