Another option is to figure out the first and last day of week within the interval and get the number of weeks . Using iTVF
create function NumDOWBetween (@StartDate DATETIME, @EndDate DATETIME, @thisday varchar(3))
returns table as
return select
nofd = case
when firstthisday > lastthisday then 0
else datediff(week, firstthisday, lastthisday) + 1 end
from (
select
wdn = datepart(dw,dateadd(d,n,'20010101')),
wds = datename(weekday,dateadd(d,n,'20010101'))
from (values (1),(2),(3),(4),(5),(6),(7)) as t(n)
) t2
cross apply(
select
firstthisday = case when datepart(dw,@startdate) <= wdn
then @startdate + wdn - datepart(dw,@startdate)
else @startdate + 7 - wdn + datepart(dw,@enddate)
end,
lastthisday = case when datepart(dw,@enddate) >= wdn
then @enddate + wdn - datepart(dw,@enddate)
else @enddate - 7 + wdn - datepart(dw,@enddate)
end
) interval
where left(wds,3) = @thisday;
go