1

I want to create a calendar date table in SQL Server for 2002 to 2011, which would minimally contain all of the dates for each year, along with whether each date is a week day or week-end day.

Any ideas on where I can find such data online? (or elsewhere)? How can I import or generate such a table?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • There are a multitude of examples on how to do this. How about searching? – Mitch Wheat Apr 29 '11 at 03:36
  • You can take a look at this question, for example. http://stackoverflow.com/questions/5635594/how-to-create-a-calender-table-for-100-years-in-sql – Lamak Apr 29 '11 at 04:20

1 Answers1

3
set datefirst 1

;with Cal as
(
  select cast('20020101' as datetime) as dt
  union all
  select dt+1
  from Cal
  where dt < cast('20111231' as datetime)
)
select
  dt as [Date],
  case datepart(dw, dt)
    when 6 then 1
    when 7 then 1
    else 0
  end  as Weekend  
from Cal
option (maxrecursion 0)
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281