SDU Tools: Weekday in same week using SQL Server T-SQL

A while back, I got a request to add a new function to our free SDU Tools for developers and DBAs. The user wanted to be able to find a particular day of the week, in a given week. So for example, if I give you a date of 30th April 2021, what is the Thursday in that week? So we've added a new function WeekdayOfSameWeek.

It takes two parameters:

@DayInTargetWeek date – any day that can be used to find the target week
@DayOfWeek int – Sunday = 1, Monday = 2, etc.

Note that the function assumes the week starts on a Sunday. I know my Asian friends (and others) consider the week to start on a Monday. It's easy to change to make it work that way.

The return value is a date.

Find out more

You can see it in action in the main image above, and in the video here. The full current version of the code is also shown below:

You can use our tools as a set or as a great example of how to write functions like these.

Access to SDU Tools is one of the benefits of being an SDU Insider, along with access to our other free tools and eBooks. Please just visit here for more info:

Latest version of the code

Note: the code might wrap when displayed below.

Leave a Reply

Your email address will not be published. Required fields are marked *