I tried out this logic with several edge cases and it seems to work.
SELECT DATEDIFF(d, @dFrom, @dTo)/7+1
+ CASE WHEN DATEPART(dw,@dFrom) IN (1,7) THEN -1 ELSE 0 END
+ CASE WHEN DATEPART(dw,@dTo) IN (1,7) THEN -1 ELSE 0 END
You can change the CASE statements depending on how you want to handle cases where the start or end date is in a weekend. In my case I'm not including the weekend if the start or end date is a Saturday or Sunday.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…