Get Total Number of a Particular Day between Two Dates

From this Set of Query we can find out the occurance of a particular day between two given dates.

if you want to get calculate total no of sat then set @DayNumber=7 and for Sun Set @DayNumber=1, Mon @DayNumber=2 and so on..

DECLARE @Count int, @Startdate Datetime,@EndDate datetime, @DayNumber int
SET @Count=0
SET @Startdate=GETDATE()-1000
SET @EndDate=GETDATE()
SET @DayNumber=7
DECLARE @StartDay Int

WHILE(@EndDate>@Startdate)
BEGIN
SET @StartDay=DATEPART(dw, @Startdate)
if(@StartDay=7)
BEGIN
SET @Count=@Count+1
SET @Startdate=@Startdate+7
END
else
BEGIN
SET @StartDay=@DayNumber-@StartDay
SET @Startdate=@Startdate+@StartDay
END
END
SELECT @Count

No comments:

Post a Comment