A way to do this is as follows:
select
dates.*
, (DATEDIFF(dd, startofperiod, endofperiod) + 1)
-(DATEDIFF(wk, startofperiod, endofperiod) * 2)
-(CASE WHEN DATENAME(dw, startofperiod) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, endofperiod) = 'Saturday' THEN 1 ELSE 0 END)
as wkdaysinperiod
, (DATEDIFF(dd, startofperiod, today) + 1)
-(DATEDIFF(wk, startofperiod, today) * 2)
-(CASE WHEN DATENAME(dw, startofperiod) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, today) = 'Saturday' THEN 1 ELSE 0 END)
as wkdaystodate
from (
select
dateadd(qq, datediff(qq,0, getdate()),0) as startofperiod
, dateadd(dd,-1,dateadd(qq, datediff(qq,0, getdate()) + 1 ,0)) as endofperiod
, convert(date,getdate()) as today
) as Dates
;
The method seen above assumes the use of English, so if that does not suit modulus can be used to remove that dependency, which is shown below.
The "base date" in SQL Server is 1900-01-01 which is a Monday, so the remaining number of days since that date after division by 7, 0 is Monday, 1, Tuesday, ... 6 Sunday. This is true regardless of server datefirst
settings.
select
dates.*, ca.*
, (DATEDIFF(dd, startofperiod, endofperiod) + 1)
-(DATEDIFF(wk, startofperiod, endofperiod) * 2)
-(CASE WHEN periodstartdaynum = 6 THEN 1 ELSE 0 END) /* Sunday */
-(CASE WHEN periodenddaynum = 5 THEN 1 ELSE 0 END) /* Saturday */
as wkdaysinperiod
, (DATEDIFF(dd, startofperiod, today) + 1)
-(DATEDIFF(wk, startofperiod, today) * 2)
-(CASE WHEN periodstartdaynum = 6 THEN 1 ELSE 0 END) /* Sunday */
-(CASE WHEN todaydaynum = 5 THEN 1 ELSE 0 END) /* Saturday */
as wkdaystodate
from (
select
dateadd(qq, datediff(qq,0, getdate()),0) as startofperiod
, dateadd(dd,-1,dateadd(qq, datediff(qq,0, getdate()) + 1 ,0)) as endofperiod
, convert(date,getdate()) as today
) as Dates
cross apply (
select
datediff(dd,0,startofperiod) % 7 as periodstartdaynum
, datediff(dd,0,endofperiod) % 7 as periodenddaynum
, datediff(dd,0,today) % 7 as todaydaynum
) ca
;