SQL Server 2012 Date Format – Determine 3rd Friday of Each Month

datedate formatsql serversql-server-2012

I need to determine the dates which are the "3rd Friday of each month" for a date range of "1.1.1996 – 30.8.2014" in SQL Server.

I expect I should use a combination of DENSE_RANK() and PARTITION BY() to set "rank = 3". However, I am new to SQL and unable to find the correct code.

Best Answer

Given:

  • Friday is called "Friday"
  • The 3rd Friday of the month will always fall from 15th-21st of the month

    select thedate
    from yourtable
    where datename(weekday, thedate) = 'Friday'
    and datepart(day, thedate)>=15 and datepart(day, thedate)<=21;
    

You could also use weekday with datepart(), but it's more readable with a name IMO. String comparisons will obviously be slower though.