在此TSQL存储过程中进行日期数学的正确方法是什么?

时间:2016-01-13 19:38:55

标签: tsql stored-procedures date-math

以下是我的商业案例:我需要根据用户输入的“BegDate”和“EndDate”的日期值来确定另外两个日期。具体来说,我需要知道:

Week1Begin (which is the value they enter for "BegDate")
Week2End (which is the value they enter for "EndDate")
Week1End (which is the value they enter for "BegDate" + 6 days)
Week2Begin (which is the value they enter for "BegDate" + 7 days)

IOW,如果用户输入以下值:

BegDate = 12/27/2015
EndDate = 1/6/2016

......其他人的价值需要计算为:

Week1End = 1/2/2016
Week2Begin = 1/3/2016

我有一个存储过程,它执行但返回Summed列中的所有Null和0,可能是因为日期数学没有包含在SP中。这是现在的SP:

CREATE PROCEDURE [dbo].[duckbilledPlatypi]
    @Unit varchar(25),
    @BegDate datetime,
    @EndDate datetime
AS

DECLARE 
@Week1End datetime,
@Week2Begin datetime

Select Ind.Description,
    @BegDate  BegDate,
    @Week1End Week1End,
    @Week1End Week2Begin,
    @EndDate EndDate,
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.QtyShipped ELSE 0 END) 
Week1Usage,
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0 END) 
Week2Usage,
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END) 
Week1Price,
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.Price ELSE 0 END) -
   SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.QtyShipped ELSE 0 END) 
UsageVariance,
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0 END) 
 -
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END) 
PriceVariance,
    (SUM(CASE WHEN Ind.InvoiceDate BETWEEN @Week2Begin AND @EndDate THEN Ind.QtyShipped ELSE 0 
END)  -
    SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END) )
    / 
NULLIF(SUM(CASE WHEN Ind.InvoiceDate BETWEEN @BegDate AND @Week1End THEN Ind.Price ELSE 0 END),0) 
PercentageOfPriceVariance
    From    InvoiceDetail Ind
    Where   Ind.Unit = @Unit
    AND Ind.InvoiceDate BETWEEN @BegDate AND @EndDate
    Group By Ind.Description

我需要添加什么来执行所需的日期数学?像这样:

. . .
Select Ind.Description,
    @BegDate  BegDate,
    DATE_ADD(BegDate,INTERVAL 6 DAY) Week1End,
    DATE_ADD(BegDate,INTERVAL 7 DAY)  Week2Begin,
    @EndDate EndDate,
. . .

或者这个:

. . .
DECLARE 
@Week1End datetime = DATE_ADD(BegDate,INTERVAL 6 DAY),
@Week2Begin datetime = DATE_ADD(BegDate,INTERVAL 7 DAY)
. . .

...或???

更新

好的,这个:

DECLARE @Week1End datetime = DATEADD(Day, 6, @BegDate),
@Week2Begin datetime = DATEADD(Day, 7, @BegDate);

......或者这个:

DECLARE @Week1End datetime = DATEADD(Day, 6, @BegDate),
@Week2Begin datetime = DATEADD(Day, 7, @BegDate)

......或者这个:

DECLARE @Week1End datetime = DATEADD(Day, 6, @BegDate);
DECLARE @Week2Begin datetime = DATEADD(Day, 7, @BegDate);

...现在是(子)问题。

更新2

它的工作原理如下:

DECLARE 
@Week1End datetime = DATEADD(Day, 6, @BegDate),
@Week2Begin datetime = DATEADD(Day, 7, @BegDate);

2 个答案:

答案 0 :(得分:2)

您想添加日期吗?

DECLARE @BeginDatePlus6 datetime = DATEADD(dd, 6, begDate)

答案 1 :(得分:2)

我认为你正在寻找这样的东西:

.
.
Select Ind.Description,
    @BegDate  BegDate,
    DATEADD(day, 6,@BegDate)  Week1End,
    DATEADD(day, 7,@BegDate)  Week2Begin, 
    @EndDate EndDate,

.
.