How to find the count of Saturday and Sunday in a year with out LOOP and CTEs
Loading
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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Muhammad Imran AnsariPosted Jan 19, 2022, 12:26 PM
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]
Nirmal DayalPosted Jan 19, 2022, 12:11 PM
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