Home > Software design >  How to get next Monday date and 3 weeks after next Monday (Sunday) date in SQL Server
How to get next Monday date and 3 weeks after next Monday (Sunday) date in SQL Server

Time:02-22

The ultimate goal is to retrieve data (scheduling data) for the 3 weeks in the future, to be accurate next Monday (if today is Monday, this week should be skipped) and 3 weeks after next Monday (should be Sunday).

I ran my code yesterday (on Sunday) and it worked as expected, but when I ran it today (on Monday), dates didn't shift to the next week, they still showing same dates as yesterday.

SELECT CONVERT(CHAR(10), DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0), 120) AS [Next Monday]
SELECT CONVERT(CHAR(10), DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0)   20, 120) AS  [3 Weeks After Next Monday (Sunday)]

Today is 2022-02-21, therefore Next Monday date should be 2022-02-28 and 3 Weeks After Next Monday (Sunday) should be 2022-03-20

CodePudding user response:

I believe the best way would be to use a resolution of days and divide and multiply out by 7, does the following work for you?

select DateAdd(day, (DateDiff(day, 0, GetDate()) / 7) * 7   7, 0) as NextMonday
  • Related