DALLAS DALLAS -4 years ago 113
SQL Question

Finding specific days between two dates

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!

Answer Source

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'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download