We want to implement the ability for employees to ask for recurring days off, whether it is once a week, every two weeks or a certain day of a certain week of the month. For example, an employee may request the last Thurday of the month, every month, into perpetuity. (not that they will be granted)
Anyway, I have a table design that looks like this:
intStaffID - Staff ID FK to tblStaff.ID
intFrequency - 1=weekly, 2=monthly
intDay - 1-7=Day of week, 1-31=Day of month
intInterval - every nth intFrequency
intWeek - Week of the month 1-4 = 1st-5th week, 6=last week
intMonth - 1-12
dtStart
dtEnd
Because some months do not have a 5th occurence of a day, if I attempt to calculate a 5th of nth day, I will be pushed into the next month, throwing off all of the remaining days, so I need to know in advance if the test day is in the 5th week or alternatively if the setting is to use the last week of the month.
Here is where I am stuck.
Ideas?
Anyway, I have a table design that looks like this:
intStaffID - Staff ID FK to tblStaff.ID
intFrequency - 1=weekly, 2=monthly
intDay - 1-7=Day of week, 1-31=Day of month
intInterval - every nth intFrequency
intWeek - Week of the month 1-4 = 1st-5th week, 6=last week
intMonth - 1-12
dtStart
dtEnd
Because some months do not have a 5th occurence of a day, if I attempt to calculate a 5th of nth day, I will be pushed into the next month, throwing off all of the remaining days, so I need to know in advance if the test day is in the 5th week or alternatively if the setting is to use the last week of the month.
Here is where I am stuck.
VB.NET:
--Set the time part to 0
SELECT @CompareDate = DATEADD(DAY, DATEDIFF(DAY, 0, @CompareDate), 0)
--Increment the starting date to the day of the week we are looking for Maximum 6 iterations
WHILE NOT (DATEPART(WEEKDAY,@StartDate) = @WeekDay)
BEGIN
SELECT @StartDate = DATEADD(DAY,1,@StartDate)
END
--Increment the starting week one week at a time until we reach the compare date
--There has to be a more efficient way
WHILE (@StartDate < @CompareDate)
BEGIN
SELECT @StartDate = DATEADD(WEEK,@Frequency,@StartDate)
END
IF (DATEDIFF(DAY, @StartDate, @CompareDate) = 0)
BEGIN
RETURN 1
END
RETURN 0
END
Ideas?