March 15, 2010 at 3:12 pm
Great point! Thanks again!
March 15, 2010 at 10:47 pm
sturner (3/12/2010)
sure, shoot me a private message with an email address.
Nah... do here... in the open where everyone can learn.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 16, 2010 at 7:42 am
ok this is providing a fun learning curve for me;
generating a calendar table's easy, but updating it to have holidays is tripping me up.
all the holidays that are based off of firsts, i have no problem generating:
--Presidents Day : third Monday of February
--Labor Day : first Monday in September
i can find the first monday, and add x number of days or weeks to get the right date...
my problem is finding the Last monday of a given month.
for example, i want Memorial day:
--Memorial Day : last Monday in May
i can find the last day of the month, but the last Monday/day of week has got me stumped;
I thought i'd use row number aganst the table to find all the mondays in september, but some years have rownumber=5 for the last monday, the others have rownumber=4;
here's my calendar example, and the code i used to get all the mondays for may over a bunch of years:
CREATE TABLE Calendar (
TheDate datetime not null primary key,
[DayOfWeek] varchar(50),
IsHoliday bit default 0,
IsWorkHoliday bit default 0,
HolidayName varchar(100) )
declare @NumberOfYears int
SET @NumberOfYears = 50 --x years back, and x years into the future
--ten years before and after todays date:
;WITH TallyCalendar AS
(
SELECT dateadd( dd,(-365 * @NumberOfYears) + RW ,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)) As N
FROM (
SELECT TOP (730 * @NumberOfYears)
row_number() OVER (ORDER BY sc1.id) AS RW
FROM Master.dbo.SysColumns sc1
CROSS JOIN Master.dbo.SysColumns sc2
) X
)
INSERT INTO dbo.Calendar(TheDate,[DayOfWeek])
SELECT
TallyCalendar.N,
datename(dw,TallyCalendar.N)
FROM TallyCalendar
--find all mondays in may
select row_number() OVER (partition by year(TheDate),datename(dw,theDate) order by TheDate) As RW,* from calendar where month(theDate) = 5 and
datename(dw,theDate) = 'Monday'
i'm just not intuitively seeing how i would update my Calendar table.
Lowell
March 16, 2010 at 8:26 am
The last Monday of the month is nothing more than the first Monday of next month minus 7 days.
The probability of survival is inversely proportional to the angle of arrival.
March 16, 2010 at 8:30 am
[facepalm] oh snap!
so obvious when someone rubs your nose in it; thanks
Lowell
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply