Karl Arsenault
Karl Arsenault

Reputation: 11

Looking for calendar data to import into SQL Server

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?

Upvotes: 1

Views: 696

Answers (1)

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

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)

Upvotes: 3

Related Questions