2014-12-05 108 views
1

我必須找到開始日期&結束日期之間的星期六和星期日總數。我如何獲得日期範圍內的週末天數

例子#1:

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

例2:

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

任何人都可以提出請。

回答

1

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]) 

這裏是你的結果

enter image description here

0

您可以使用日期部分http://msdn.microsoft.com/en-us/library/ms174420.aspx

一個例子;

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 

星期六將7和週日將是1,所以你可以檢查旁邊的計數。

+0

這不工作.... – sk7730 2014-12-05 07:38:32

0

試試這個:

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

我們正在做的是從你開始建立天的列表結束日期,然後計算這些日期的週末。一個表變量用來存儲這個列表。那應該注意

2點:

  1. 理論上,應該使用dateadd函數來執行日期時間計算,而不是+操作。
  2. 雖然我爲了清晰起見使用了datename,但datepart會更好,因爲它會給出數值,而datename會給出與語言相關的值。
0

試試這個:

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'); 
-1
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 
+0

以上解決方案工作正常。 – sk7730 2014-12-05 10:03:34

1

今天有同樣的問題。我到了這裏。

如果您不想使用遞歸(CTE)或while。你可以使用數學加例時:

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 

我希望它通過評論變得清晰。讓我知道它是否有幫助。

相關問題