我几乎已经阅读了所有问题,我必须忽略一些简单的事情,但我无法正确计算所有情况。我有一个日期,以及我需要添加到每行特定日期的不同整数的引用表。因此,为了这个问题,我只是专门定义它们而不是更复杂的查询。
对于日期A,我需要在该日期添加7天,但不包括周末 对于日期B,我需要添加5天,但与上述相同,不包括周末。
我已经从这里引用了代码,
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2008/10/01'
SET @EndDate = '2008/10/31'
SELECT
(DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
并使用我自己的逻辑来应用它
SELECT '11/05/2015' AS [A Begin]
, 7 AS [A Add]
, CAST(DATEADD(day, ((DATEDIFF(wk, '11/05/2015', DATEADD(day, 7, '11/05/2015')) * 2)
+ (CASE WHEN DATENAME(dw, '11/05/2015') = 'Sunday' THEN 1 ELSE 0 END)
+ (CASE WHEN DATENAME(dw, DATEADD(day, 7, '11/05/2015')) = 'Saturday' THEN 2 ELSE 0 END) + 7), '11/05/2015') AS DATETIME)
+ CAST('16:00' AS DATETIME) AS [A End]
, '11/05/2015' AS [B Begin]
, 5 AS [B Add]
, CAST(DATEADD(day, ((DATEDIFF(wk, '11/05/2015', DATEADD(day, 5, '11/05/2015')) * 2)
+ (CASE WHEN DATENAME(dw, '11/05/2015') = 'Sunday' THEN 1 ELSE 0 END)
+ (CASE WHEN DATENAME(dw, DATEADD(day, 5, '11/05/2015')) = 'Saturday' THEN 2 ELSE 0 END) + 5), '11/05/2015') AS DATETIME)
+ CAST('16:00' AS DATETIME) AS [B End]
对于范围A,它增加了7天,但在星期六登陆作为结束..我不能拥有,它需要等于11/16/2015才是正确的。范围B工作得很好。我无法执行声明或任何其他功能。