How do I get count of weekend days from a range of dates

我怕爱的太早我们不能终老 提交于 2019-12-24 12:34:37

问题


I have to find out total number of saturday and sunday between Start Date & End Date.

Example #1:

StartDate = Getdate(), EndDate = GetDate() + 5      -- result should be 2.

Example #2:

StartDate = Getdate(), EndDate = GetDate() + 10     -- result should be 4.

Can anyone suggest please.


回答1:


Here it is

DECLARE @STARTDATE DATE='01/JAN/2014'    
DECLARE @ENDDATE DATE='01/MAR/2014'

;WITH  CTE as
(
    SELECT  CAST(@STARTDATE AS DATE) as [DAYS] 
    UNION ALL
    SELECT DATEADD(DAY,1,[DAYS]) [DAYS]
    FROM    CTE
    WHERE   [DAYS] < CAST(@ENDDATE AS DATE)
)
SELECT DISTINCT COUNT([DAYS]) OVER(PARTITION BY DATENAME(WEEKDAY,[DAYS])) CNT,
DATENAME(WEEKDAY,[DAYS]) WD
FROM CTE 
WHERE DATENAME(WEEKDAY,[DAYS]) = 'SATURDAY' OR DATENAME(WEEKDAY,[DAYS]) = 'SUNDAY'
ORDER BY DATENAME(WEEKDAY,[DAYS]) 
  • SQL FIDDLE

Here is your result




回答2:


Had the same question today. And I got here.

If you don't want to use recursion (CTE) or while. You can use math plus Case When:

DECLARE @StartDate AS DATE
DECLARE @EndDate AS DATE
SET @StartDate = Getdate()
SET @EndDate = GetDate() + 11

SELECT
    -- Full WE (*2 to get num of days Sa and So)             
    (((DATEDIFF(d,@StartDate,@EndDate)+1)/7)*2)
    +
    -- WE-Days in between; given that Saturday = 7 AND Sunday = 1
    -- what if startdate is sunday And you have remaining Days; you will always only get one WE-day
    CASE WHEN DATEPART(dw,@StartDate) = 1 AND (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 THEN 1
        -- If you have remaining days (Modulo 7 > 0) and the sum of number of starting day and remaining days is 8 (+1 for startingdate) then you have + 1 WE-day (its a saturday)  
        ELSE CASE WHEN (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 AND (((DATEDIFF(d,@StartDate,@EndDate)+1)%7) + DATEPART(dw,@StartDate)) = 8 THEN 1
            -- If the remaining days + the number of the weekday is are greater then 8 (+1 for startingdate) you have 2 days of the weekend in between.  
            ELSE CASE WHEN (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 AND (((DATEDIFF(d,@StartDate,@EndDate)+1)%7) + DATEPART(dw,@StartDate)) > 8 THEN 2
            -- you have no WE-days in between! Either because of the fact that you have a number that is divisable by 7 or because the remaining days are between 2 (Tuesday) and 6 (Friday)   
            ELSE 0
            END
        END
    END   AS TotalWEDays

I hope it gets clear by the comments. Let me know if it helps.




回答3:


You can use Datepart http://msdn.microsoft.com/en-us/library/ms174420.aspx

An example;

WITH CTE as(
Select DATEPART(WeekDay,MyDate) as DP From Table Where Mydate > @StartDate and MyDate < @EndDate)
Select Count(*) as CT,DP From CTE
group by DP

Saturday will be 7 and Sunday will be 1 so you can check the count next to them.




回答4:


Try this:

declare @startdate datetime = getdate()
declare @days int = 5
declare @cal table(dt datetime)

declare @counter int = 0
while @counter < @days
   begin
   insert into @cal values (@startdate + @counter) --Ideally should be dateadd(dd,@counter,@startdate)
   set @counter = @counter + 1
end

select count(*) from @cal
where datename(dw,dt) = 'Saturday' or datename(dw,dt) = 'Sunday'
--Ideally should be 
--where datename(dw,dt) = 1 or datename(dw,dt) = 7

Demo

What we are doing is building up a list of days from your start to end date, and then counting the weekends from those dates. A table variable is used to store this list.

2 points that should be noted here:

  1. You should ideally use the dateadd function to perform date time calculations rather than the + operator.
  2. While I have used datename for clarity, datepart would be better since it gives numeric values while datename gives language-dependent values.



回答5:


Try this:

DECLARE @V_StartDate DATETIME = GETDATE(), @V_EndDate DATETIME = GETDATE() + 5;

WITH showDateCTE(DateCol)
AS
(
SELECT DateCol = @V_StartDate
UNION ALL
SELECT DATEADD(DAY, 1, DateCol) 
FROM showDateCTE 
WHERE DateCol < @V_EndDate - 1
)
SELECT COUNT(1) weekEndCount  
FROM showDateCTE  
WHERE DATENAME(dw, CONVERT(DATE, DateCol)) IN ('Saturday', 'Sunday');



回答6:


    declare @startdate datetime 
    declare @enddate datetime
    declare @weekendCnt int
    set @startdate = getdate()
    set @enddate = getdate()+8
    set @weekendCnt = 0

    while @startdate < @enddate
    begin
    PRINT @startdate
     if(datename(dw, @startdate) in( 'Saturday','Sunday'))
         begin
             set @weekendCnt = @weekendCnt + 1         
         end
         set @startdate = @startdate +1
     end
    print @weekendCnt


来源:https://stackoverflow.com/questions/27310160/how-do-i-get-count-of-weekend-days-from-a-range-of-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!