How to find the count of Saturday and Sunday in a year with out LOOP and CTEs
Find count of Saturday and Sunday count in a year with out loop,cte
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
How to find the count of Saturday and Sunday in a year with out LOOP and CTEs
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nirmal DayalRank #1,712 · 886 reputationPosted Jan 19, 2022, 7:11 AM EST
This query helps to you
Declare @StartDate as datetime = '01/Jan/2022'
Declare @EndDate as datetime = Getdate()
select count(*) as Daycount
from master..spt_values as Number
where Number.type = 'P' and
dateadd(day, Number.number, @StartDate) <= @EndDate and
datepart(dw, dateadd(day, Number.number, @StartDate)) = 5
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 19, 2022, 7:26 AM EST
You can try the following query:
DECLARE @startdate DATETIME = '2022-01-01'
DECLARE @enddate DATETIME = '2022-01-31'
DECLARE @TotalSundays INT
DECLARE @TotalOtherDays INT
SET @TotalSundays = (SELECT DATEDIFF(DAY,@startdate,@enddate) /7 +
CASE
WHEN DATEPART(WEEKDAY,@StartDate) = 1 THEN 1
WHEN DATEPART(WEEKDAY,@StartDate) > DATEPART(WEEKDAY,@EndDate) THEN 1
ELSE 0
END)
SELECT @TotalSundays [Total Sundays]