6

如何在案例中嵌入和 if 语句。这就是我到目前为止所拥有的。

DECLARE @LDCCode as int
       DECLARE @InvoiceDate as datetime
       DECLARE @PaymentSemiMonthlyDays AS int
       SET @LDCCode = 20
       SET @InvoiceDate = '5/16/2012'
       DECLARE @InvDateDayMonth AS INT
       DECLARE @ReturnDate AS DATETIME
       SET @ReturnDate = @InvoiceDate
       DECLARE @PaymentDOM AS INT
       DECLARE @PaymentDays AS INT
       DECLARE @PaymentSemiMonthlyOffset AS INT


SET @ReturnDate = CASE WHEN NOT @PaymentDOM IS NULL THEN
   @ReturnDate + (@PaymentDOM - DATEPART(day, DateAdd(mm,1,@ReturnDate))) 
WHEN NOT @PaymentDays IS NULL THEN 
            DATEADD(Day,@PaymentDays,@ReturnDate)
WHEN NOT @PaymentSemiMonthlyOffset IS NULL THEN                     
    IF @LDCCode = 40 AND @InvDateDayMonth > 11 AND @InvDateDayMonth < 26 
       SELECT @ReturnDate + (@PaymentSemiMonthlyOffset - DATEPART(day, DateAdd(mm,1,@ReturnDate)))
    ELSE
       SELECT @ReturnDate + (@PaymentSemiMonthlyDays - DATEPART(day, DateAdd(mm,1,@ReturnDate)))    
    END 
4

2 回答 2

8

您不能以这种方式组合逻辑,但您可以添加一个嵌套CASE的现有CASE语句:

SET @ReturnDate = (CASE WHEN NOT @PaymentDOM IS NULL THEN
    @ReturnDate + (@PaymentDOM - DATEPART(day, DateAdd(mm,1,@ReturnDate))) 
WHEN NOT @PaymentDays IS NULL THEN 
    DATEADD(Day,@PaymentDays,@ReturnDate)
WHEN NOT @PaymentSemiMonthlyOffset IS NULL THEN
    CASE WHEN @LDCCode = 40 AND @InvDateDayMonth > 11 AND @InvDateDayMonth < 26 THEN
        @ReturnDate + (@PaymentSemiMonthlyOffset - DATEPART(day, DateAdd(mm,1,@ReturnDate)))
    ELSE
        @ReturnDate + (@PaymentSemiMonthlyDays - DATEPART(day, DateAdd(mm,1,@ReturnDate)))
    END   
END)

但是,如果这是在存储过程中,我可能会选择更简单IF/THEN/ELSE的结构而不是CASE

IF @PaymentDOM IS NULL
    SET @ReturnDate = @ReturnDate + (@PaymentDOM - DATEPART(day, DateAdd(mm, 1, @ReturnDate))) 
ELSE IF NOT @PaymentDays IS NULL
    SET @ReturnDate = DATEADD(Day,@PaymentDays,@ReturnDate)
ELSE IF NOT @PaymentSemiMonthlyOffset IS NULL
BEGIN
    IF @LDCCode = 40 AND @InvDateDayMonth > 11 AND @InvDateDayMonth < 26
        SET @ReturnDate = @ReturnDate + (@PaymentSemiMonthlyOffset - DATEPART(day, DateAdd(mm, 1, @ReturnDate)))
    ELSE
        SET @ReturnDate = @ReturnDate + (@PaymentSemiMonthlyDays - DATEPART(day, DateAdd(mm, 1, @ReturnDate)))
END
于 2012-05-08T19:57:55.853 回答
2

由于所有表达式都添加了一些东西,并且由于您多次@ReturnDate计算一个月中的哪一天,我认为这可以简化为:@ReturnDate

DECLARE @Plus1M INT;
SET @Plus1M = DATEPART(DAY, DATEADD(MONTH, 1, @ReturnDate));

SET @ReturnDate = DATEADD(DAY, 
  COALESCE(
    @PaymentDays, -- if @PaymentDays IS NULL, this will be skipped
    @PaymentDOM - @Plus1M,  -- if @PaymentDOM is NULL, this will be skipped
    CASE WHEN @PaymentSemiMonthlyOffset IS NOT NULL THEN
      CASE WHEN @LDCCode = 40 AND @InvDateDayMonth BETWEEN 12 AND 25
        THEN @PaymentSemiMonthlyOffset 
        ELSE @PaymentSemiMonthlyDays 
      END - @Plus1M
    END
  ), 
  @ReturnDate
);

但是如果你想把它写成冗长和冗余的表达式,那么:

SET @ReturnDate = CASE 
  WHEN @PaymentDOM IS NOT NULL THEN
    DATEADD(DAY, @PaymentDOM 
    - DATEPART(DAY, DATEADD(MONTH, 1, @ReturnDate)), @ReturnDate) 
  WHEN @PaymentDays IS NOT NULL THEN 
    DATEADD(DAY, @PaymentDays, @ReturnDate)
  WHEN @PaymentSemiMonthlyOffset IS NOT NULL THEN
    CASE WHEN @LDCCode = 40 AND @InvDateDayMonth BETWEEN 12 AND 25 THEN
      DATEADD(DAY, @PaymentSemiMonthlyOffset 
      - DATEPART(DAY, DATEADD(MONTH, 1, @ReturnDate)), @ReturnDate)
    ELSE
      DATEADD(DAY, @PaymentSemiMonthlyDays 
      - DATEPART(DAY, DATEADD(MONTH, 1, @ReturnDate)), @ReturnDate)   
    END 
  END;

但是很难验证,因为您现有的查询无法解析,并且您没有向我们展示示例数据或所需的结果。

我会避免使用@Datetime + INT速记,而宁愿拼出DATEADD操作,因为速记不适用于较新的类型(DATE,DATETIME2等)。我也会避免使用速记,mm因为其中一些缩写并不明显。另外三个字符使这一点更加明显。而且NOT ...something... IS NULL阅读起来也不是很直观。我宁愿说“灯不是红的”而不是“灯不是红的”。

于 2012-05-08T20:12:01.620 回答