SDU Tools: Nearest Weekday using SQL Server T-SQL
I spend a lot of time doing data conversion work. It’s hard to imagine how much of that relates to working with dates and times. A requirement that I had recently was to find the nearest weekday to a given date i.e. find me the closest Thursday to a supplied date.
The NearestWeekday function in our free SDU Tools for developers and DBAs, does this.
It takes two parameters:
@TargetDate date - the date that we’re aiming for @DayOfWeek int - Sunday = 1, Monday = 2, Tuesday = 3, etc. (values from 1 to 7)
2021-01-22

