snippetsqlMajor
Determine 3rd Friday of each month
Viewed 0 times
each3rdfridaymonthdetermine
Problem
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
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.Solution
Given:
-
The 3rd Friday of the month will always
fall from 15th-21st of the month
You could also use
- 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.Code Snippets
select thedate
from yourtable
where datename(weekday, thedate) = 'Friday'
and datepart(day, thedate)>=15 and datepart(day, thedate)<=21;Context
StackExchange Database Administrators Q#120488, answer score: 28
Revisions (0)
No revisions yet.