SDU Tools: Calculate week day of the month in SQL Server T-SQL

The DateDiffNoWeekends tool in our free SDU Tools for developers and DBAs, has been really popular. Another request that we received though, was how to calculate which date a particular numbers week day is. For example, we might want to know which is the 12th week day of the month. So we've added a tool to do that. It's called WeekDayOfMonth.

It's a function that takes three parameters:

@Year is the year (of the month in question)
@Month is month in question (as a number within a calendar year i.e. March = 3)
@WeekdayNumber is an integer that gives you the position in the month that you are trying to calculate.

One nice advantage of this function is that you can supply a negative @WeekdayNumber parameter, and it will start counting from the end of the same month instead.

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

Find out more

You can see it in action in the main image above, and in the video here:

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:

http://sdutools.sqldownunder.com

 

Leave a Reply

Your email address will not be published.