Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get Friday dates in a month? Suppose in July there are 4 Friday. I want to retrieve the dates. I don't use any database table. Please help me.
Posted

try this.. :)

SQL
declare @DateFrom datetime,@DateTo datetime
set @DateFrom='23 Mar 2014'
set @DateTo='26 Jun 2014'

;WITH CTEQuery AS (
			  SELECT CAST(@DateFrom AS DATETIME) AS dt
			  UNION ALL
			  SELECT DATEADD(dd, 1, dt)
			   FROM CTEQuery s
			   WHERE DATEADD(dd, 1, dt) <= CAST(@DateTo AS DATETIME)
			   ),sampleData as(
select dt,datename(WEEKDAY,dt)as [DayName] from CTEQuery )
select * from sampleData where [DayName]='Friday'


you have to just pass @DateFrom and @DateTo from code behind and you will get all friday dates between there dates.. :)
 
Share this answer
 
Hi,

First of all, I'm assuming that you're using SQL Server. Here's an example (the query should work in SQL Server 2005 or above):
SQL
DECLARE @Year      VARCHAR(4),
        @Month     VARCHAR(3),
        @FirstDate DATETIME,
        @LastDate  DATETIME;

SET @Year = '2014';
SET @Month = '07'; -- '07' or 'Jul'
SET @FirstDate = (SELECT CAST(( @Year + @Month + '01' ) AS DATETIME));
SET @LastDate = DATEADD(DAY, -1, ( DATEADD(MONTH, 1, @FirstDate) ));

;WITH Fridays([Date])
     AS (SELECT [Date] = @FirstDate
         UNION ALL
         SELECT [Date] = DATEADD(DAY, 1, [Date])
         FROM   Fridays
         WHERE  [Date] < @LastDate)
SELECT [Date]
FROM   Fridays
WHERE  DATENAME(WEEKDAY, [Date]) = 'Friday';

Note: This query could (should) be improved considering your input parameters and version of the SQL Server.

Result:
XML
Date
2014-07-04 00:00:00.000
2014-07-11 00:00:00.000
2014-07-18 00:00:00.000
2014-07-25 00:00:00.000


Please let me know if you need further assistance.
 
Share this answer
 
v3
SQL
DECLARE @StartDate date = '2014-07-01'
DECLARE @EndDate date = '2014-07-31' 
CREATE TABLE #tblGetFriday ([Friday] Date)

WHILE @StartDate <> CONVERT(date,@EndDate)
BEGIN
	IF((SELECT UPPER(DATENAME(dw, @StartDate))) = 'FRIDAY')
	BEGIN	
		INSERT INTO #tblGetFriday SELECT @StartDate As Fridays
	END
	
	
	SET @StartDate = DATEADD(Day,1,@StartDate)
END

SELECT * FROM #tblGetFriday
DROP TABLE #tblGetFriday
 
Share this answer
 
v2
try this and make your logic
C#
public static List<datetime> GetDates(int year, int month)
{
   var dates = new List<datetime>();

   // Loop from the first day of the month until we hit the next month, moving forward a day at a time
   for (var date = new DateTime(year, month, 1); date.Month == month; date = date.AddDays(1))
   {
      dates.Add(date);       
   }

   return dates;
}
</datetime></datetime>
 
Share this answer
 
v3
Comments
Maciej Los 7-Jul-14 8:31am    
I can't see C# in the list of tags.

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