Tuesday, May 25, 2004

Next Day

This chunk of T-SQL code can find the next date that a certain day of the week occurs after the current date. The example finds the date of the next Thursday.

-- Get the next Thursday (in the formula we are using Monday = 1 so Thursday = 4)
SET @wanted_day = 4
SET @ap_date = DATEADD(d, (15 + @wanted_day - @@datefirst - DATEPART(dw, GETDATE())) % 7, GETDATE())