0

I need help to find out how many Fridays there are between two specific dates. I have found and read through the post concerning; Work days between two dates - but I'm not able to crack how to count specific days between two dates. I need a more reliable solution than just dividing it by 7.

Thanks!

Community
  • 1
  • 1
DALLAS
  • 21
  • 2
  • 1
    You should tag your question with the database you are using. – Gordon Linoff Feb 28 '17 at 11:57
  • You should also post the failing code. – Ejaz Feb 28 '17 at 11:58
  • And also define “a more reliable solution than just dividing it by 7”. As long as a week has seven days, that number is going to come into play in one way or another. – CBroe Feb 28 '17 at 11:59
  • @CBroe, an 8 day period can contain 1 or 2 Fridays. – jarlh Feb 28 '17 at 12:00
  • @jarlh that could be "one way" or the "other way" CBore is talking about. – Ejaz Feb 28 '17 at 12:01
  • @GordonLinoff Sorry, I have added this. Thanks for your reply – DALLAS Feb 28 '17 at 12:01
  • @Ejaz I have only been trying around with the code from the link I posted, so I don't have any code to post at this moment sorry. – DALLAS Feb 28 '17 at 12:04
  • Would following method work? `if first day of interval is friday, then, number_of_fridays = 1 ; number_of_fridays += integer value of (number of days in interval % 7);`?... or gives some direction? – Ejaz Feb 28 '17 at 12:08
  • @jarlh: Yes, of course. What day of week the start or end date (depending which side you start) actually is, has to be taken into account of course. – CBroe Feb 28 '17 at 12:08
  • @DavidG Thanks, thats exactly what I wanted! – DALLAS Feb 28 '17 at 12:09

1 Answers1

0

declare @startDate date;
declare @endDate date;

select @startDate = '2017-02-01';
select @endDate = '2017-02-10';

with dateBetween as
(
  select dt = dateadd(dd, 1, @startDate)
  where dateadd(dd, 1, @startDate) < @endDate
  union all
  select dateadd(dd, 1, dt)
  from dateBetween
  where dateadd(dd,1, dt) < @endDate
)

select DATENAME(dw,dt) As [Dayname],dt as [Date]
from dateBetween where DATENAME(dw,dt) ='Friday'
Singh Kailash
  • 621
  • 5
  • 16