To add to GarethD's answer - I put together the SQL for a USA version of the Calendar table, with all holidays and weekends set to is_working_day = false... for anyone that would like the SQL, here it is:
declare @start_dt as date = '1/1/2009'; -- Date from which the calendar table will be created.
declare @end_dt as date = '1/1/2030'; -- Calendar table will be created up to this date (not including).
create table CalendarTable (
date_id date primary key,
date_year smallint,
date_month tinyint,
date_day tinyint,
weekday_id tinyint,
weekday_nm varchar(10),
month_nm varchar(10),
day_of_year smallint,
quarter_id tinyint,
first_day_of_month date,
last_day_of_month date,
start_dts datetime,
end_dts datetime,
week_number_of_month int,
is_working_day bit,
)
while @start_dt < @end_dt
begin
insert into CalendarTable(
date_id, date_year, date_month, date_day,
weekday_id, weekday_nm, month_nm, day_of_year, quarter_id,
first_day_of_month, last_day_of_month,
start_dts, end_dts, week_number_of_month, is_working_day
)
values(
@start_dt, year(@start_dt), month(@start_dt), day(@start_dt),
datepart(weekday, @start_dt), datename(weekday, @start_dt), datename(month, @start_dt), datepart(dayofyear, @start_dt), datepart(quarter, @start_dt),
dateadd(day,-(day(@start_dt)-1),@start_dt), dateadd(day,-(day(dateadd(month,1,@start_dt))),dateadd(month,1,@start_dt)),
cast(@start_dt as datetime), dateadd(second,-1,cast(dateadd(day, 1, @start_dt) as datetime)), DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, @start_dt), 0), @start_dt) +1, 0
)
set @start_dt = dateadd(day, 1, @start_dt)
end
GO
-- Set all non-weekend days as business days
update CalendarTable set is_working_day = 1 where weekday_id not in (1,7)
GO
-- New Years Day
update CalendarTable set is_working_day = 0 where date_month = 1 and date_day = 1
GO
-- Memorial Day (last Monday of May)
WITH Memorial_Day AS
(
SELECT date_id, date_year, date_day,
ROW_NUMBER() OVER (PARTITION BY date_year ORDER BY date_day desc) AS RowNumber
FROM CalendarTable
where date_month = 5 and weekday_id = 2
)
update CalendarTable set is_working_day = 0 where date_id in (SELECT date_id FROM Memorial_Day
where rownumber = 1)
GO
-- Independence Day
update CalendarTable set is_working_day = 0 where date_month = 7 and date_day = 4
GO
-- Labor Day (first Monday in September)
WITH Labor_Day AS
(
SELECT date_id, date_year, date_day,
ROW_NUMBER() OVER (PARTITION BY date_year ORDER BY date_day) AS RowNumber
FROM CalendarTable
where date_month = 9 and weekday_id = 2
)
update CalendarTable set is_working_day = 0 where date_id in (SELECT date_id FROM Labor_Day
where rownumber = 1)
GO
-- Thanksgiving (fourth Thursday in November)
WITH Thanksgiving AS
(
SELECT date_id, date_year, date_day,
ROW_NUMBER() OVER (PARTITION BY date_year ORDER BY date_day) AS RowNumber
FROM CalendarTable
where date_month = 11 and weekday_id = 5
)
update CalendarTable set is_working_day = 0 where date_id in (SELECT date_id FROM Thanksgiving
where rownumber = 4)
GO
-- Day After Thanksgiving (fourth Friday in November)
WITH DayAfterThanksgiving AS
(
SELECT date_id, date_year, date_day,
ROW_NUMBER() OVER (PARTITION BY date_year ORDER BY date_day) AS RowNumber
FROM CalendarTable
where date_month = 11 and weekday_id = 6
)
update CalendarTable set is_working_day = 0 where date_id in (SELECT date_id FROM DayAfterThanksgiving
where rownumber = 4)
GO
-- Christmas Day
update CalendarTable set is_working_day = 0 where date_month = 12 and date_day = 25
GO