Find last Friday in SQL Server 2008

Microsoft SQL Server has very rich tools for date and time but just due to unawareness people find it difficult to make operation on datetime in SQL Server and used to go to front-end languages like .NET. I used to prefer SQL Server for performance issue. So far I have written quite a few articles on date time operation in SQL Server which is as follow.
If you will go through above given articles, you will come to know that datetime functions are really very handy and useful. Anyway, the main purposes to write this article to show you one another very small T-SQL for find out last Friday of the day.
SELECT DATEADD(day, (DATEDIFF (day, ‘19800104’, CURRENT_TIMESTAMP) / 7) * 7, ‘19800104’) as ‘Last Friday’



It was really very easy. Right?
Actually the logic behind this small query is, there was a Friday on 4th Jan 1980 so I am getting difference between 4th Jan 1980 to current date, dividing it with 7 than multiplying with 7 and whatever number comes, I am adding it to the same date and I will get last Friday.
Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder ofhttp://www.SQLHub.com
Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah

Find first and last Friday of every month in SQL Server 2008/2005

Today, I helped someone in one of the forum about above question. He wanted to find first and last Friday of every month from 1st Jan 2006 to 31st Dec 2020. This is bit a challenging job. There are quite a few ways to do so like temp table, CTE etc. and I have had my hand in CTE to solve this problem. Let us see the script itself.
WITH CTE AS
(
select CONVERT(datetime,’01/06/2006′) as dt,  datename(dw,CONVERT(datetime,’01/06/2006′)) as dy,datename(mm,CONVERT(datetime,’01/06/2006′)) as mn, YEAR(CONVERT(datetime,’01/06/2006′)) AS yr
union all
select s.dt+7 as dts,datename(dw,dt+7) as dy  ,datename(mm,dt+7) as dy, YEAR(dt+7) AS yr
from cte s
where s.dt<CONVERT(datetime,’12/31/2020′)
)
select MAX(dt) as dt,dy,mn,yr from cte
group by dy,mn,yr
UNION
select MIN(dt) as dt,dy,mn,yr from cte
group by dy,mn,yr
ORDER BY dt



Quite a simple to do so in CTE but if you will try to attempt above query, you will be greeted by following error.
Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.



How to fix that annoying error? It is crossing default recursion limit of SQL Server which is 100 and above CTE will iterate much more time than 100 so obviously you are going to face an error. All you need to do is, just have one clause after above CTE, OPTION (MaxRecursion 0).  So finally query will looks like
WITH CTE AS
(
select CONVERT(datetime,’01/06/2006′) as dt,  datename(dw,CONVERT(datetime,’01/06/2006′)) as dy,datename(mm,CONVERT(datetime,’01/06/2006′)) as mn, YEAR(CONVERT(datetime,’01/06/2006′)) AS yr
union all
select s.dt+7 as dts,datename(dw,dt+7) as dy  ,datename(mm,dt+7) as dy, YEAR(dt+7) AS yr
from cte s
where s.dt<CONVERT(datetime,’12/31/2020′)
)
select MAX(dt) as dt,dy,mn,yr from cte
group by dy,mn,yr
UNION
select MIN(dt) as dt,dy,mn,yr from cte
group by dy,mn,yr
ORDER BY dt
OPTION (MaxRecursion 0)

Enjoy!!!
Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com
Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah