Given the following database table:
StartDate DATETIME -- day that the reminder period starts
LastReminderDate DATETIME -- the last time the reminder triggered
DayOfMonth INT -- the day of the month to remind the user
Interval INT -- how often in months to remind the user
How can I figure out the next reminder date based on these values? For example:
StartDate = '6/1/2011'
LastReminderDate = '6/5/2011'
DayOfMonth = 5 -- remind on the 5th of the month
Interval = 2 -- remind every other month
For this particular example, the next reminder date should be 8/5/2011 because it reminds on the 5th of the month every two months. How would I write a function to figure this out?
If LastReminderDate is NULL, then LastReminderDate should be equal to StartDate
UPDATE:
StartDate = '6/1/2011'
LastReminderDate = NULL
DayOfMonth = 5
Interval = 2
In this case, there was no last reminder date. The first time the reminder would occur would be 6/5/2011. The solutions below seem to be returning 8/5 in this case.
Here are some specific rules:
- The Reminder should always occur on whatever DayOfMonth is. If DayOfMonth would be illegal for the given month then it should be the last day of that month. For example….if DayOfMonth is 31 and the next reminder date would fall on June 31, then it should be June 30th instead.
- The next reminder date should always be based off of the Last Reminder Date plus the Interval. If Last Reminder Date does not match the Day of Month, then it could potentially be more than what the interval was. For example…if Last Reminder was 6/1/2011 and the interval is 2 months, but the reminder is for the 20th of the month, then the next reminder will be 8/20/2011.
- If there is no last reminder date, then use the Start Date instead of last reminder date…but this will use the earliest date in the future. If start date was 6/1/2011 and day of month is 5, then this will be 7/5/2011 since today is 6/22/2011. If Day of Month was 25 then it would be 6/25/2011
The meat of this is the last four lines, the
SELECT CASE ... ENDstatement. I provided a whole script that lets you plug in different values, and see how theSELECT CASE ... ENDbehaves for those test values.But to just use this on your table, use only the last four lines (and remove the
@from the front of the names so they match the table’s column names).You could also generalize this so that Interval doesn’t have to be months. If your table had an IntervalType column, you could supply that as the first argument to
DateAdd(). See the docs but some common intervals aredays,months,years, and so on.EDIT2: Respect DayOfMonth.