Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi

I want to get the all firdays date for a particular month of a year in sql server .
Please help me out to design a query for that.

thanx in advance.
Posted
Comments
Member 8244101 14-Dec-11 3:30am    
Hi thnx for your valuable suggestions.

Actually i want them(friday's date) in a single row.

The first problem is that the amount of Fridays per each month is changing. Sometimes you have four and sometimes five.

One possibility is to have a table which contains all dates and you restrict dates from that table for the desired period and use DATEPART[^] to check the day of the week (if it's Friday). Something like:
SQL
SELECT [Date]
FROM AllDates
WHERE [Date] BETWEEN @start AND @end
AND DATEPART(dw, [Date]) = 6;


Another way could be using table valued functions to generate the rows. For example see: Using Table-Valued Functions in SQL Server[^]
 
Share this answer
 
Try this :

SQL
declare @year int 
set  @year  = 2011
declare @month int 
set  @month   = 12
declare @sd as datetime
declare @fd as datetime
set @sd = CAST(CAST(@year AS varchar) + '-' + CAST(@month AS varchar) + '-1' AS DATETIME)
set @fd = DATEADD(DAY, -(DAY(DATEADD(MONTH, 1, @sd))), DATEADD(MONTH, 1, @sd))


while @sd<=@fd 
begin
if datepart(dw, @sd) = 6 
 print cast (@sd as varchar(max))  +  ' is Friday.'
 set @sd = DATEADD(DAY, 1, @sd)
end;


Good Luck.
 
Share this answer
 
Comments
Member 8244101 14-Dec-11 3:31am    
Hi thnx for your valuable suggestions. Actually i want them(friday's date) in a single row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900