如果需要准确,
STEP01 减去 2 乘以数字整数(DATEDIFF RESULT)除以 7
STEP02 THEN 取 7 减去工作日(START DATE)数字(USE DATEPART)
STEP03 如果大于数字(DATEDIFF RESULT ) ) ,减去 2。
DECLARE @start AS datetime = '10/24/2014'
DECLARE @end AS datetime = '10/31/2014'
SELECT CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS int)AS STEP_ONE_OrigDays
--,CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)*(1.000*(5.000/7.000)) AS EstDays
,(2.000*(CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)/7)) AS STEP_TWO--SUBTRACT 2 times the number integer divided by 7
,DATEPART(dw, @start) AS STEP_THREE_WEEK_DAY_NUMBER --WEEK DAY #
,7 - DATEPART(dw, @start) AS STEP_FOUR_SEVENminus_WEEK_DAY_NUMBER --7 minus WEEK DAY #
,CAST(CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)
-(2.000*(CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)/7))
+CASE
WHEN (7.000-DATEPART(dw, @start)) > CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)
THEN -2.000
ELSE 0.000
END AS int) AS ExactDays_NOT_IncludingDayOf --JAH EXCLUDE WEEKEND Fx NOT Including the Day of
,CAST(CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)-(2.000*(CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)/7))
+CASE
WHEN (7.000-DATEPART(dw, @start)) > CAST(DATEDIFF("d",CAST(@start AS datetime),CAST(@end AS datetime)) AS money)
THEN -2.000
ELSE 0.000
END AS int)+1 AS ExactDays_IncludingDayOf --JAH EXCLUDE WEEKEND Fx INCLUDING the Day of