Sunday, March 25, 2012

consecutive dates

Forgive the primitive nature of this question, but I'm just trying to do a list of dates (without weekends and perhaps without major holidays). Any help appreciated. (Excel Help said to use the autofill feature, but I can't find it!
Thanks,

Bill Fried

I would start with this article about "calendar tables:"

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

One way to get a list of calendar dates is to use a table of numbers and generate the list of dates by incrementing through the list of numbers. Something like this:

select cast('12/31/6' as datetime) + num

from numbers (nolock)

where num <= 365

|||

The following function will do.. The performance is not bad..

Code Snippet

Create Function dbo.FillDate(@.Startdate datetime, @.Enddate datetime)

Returns @.Data Table (Date DateTime)

as

Begin

While @.Startdate <= @.EndDate

Begin

Insert Into @.Data Select @.Startdate Where Datepart(w,@.Startdate) not in (1,7);

Select @.Startdate = Dateadd(dd,1,@.Startdate);

End

return;

End

Go

Select * from FillDate('1900-01-01','2007-12-31')

No comments:

Post a Comment