简体   繁体   English

如果声明内部案例嵌入

[英]Embed If Statement Inside Case

How to embed and if statement inside a case. 如何在案例中嵌入和if语句。 This is what I have so far. 这就是我到目前为止所拥有的。

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 

You can't combine logic in that manner, but you can add a nested CASE your existing CASE statement: 您不能以这种方式组合逻辑,但您可以在现有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)

Though, if this is in a stored procedure, I might just opt for simpler IF/THEN/ELSE structure instead of the CASE : 但是,如果这是在存储过程中,我可能只选择更简单的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

Since all of the expressions add something to @ReturnDate , and since you calculate the day of a month after @ReturnDate multiple times, I think this can be simplified as: 由于所有表达式都向@ReturnDate添加了一些@ReturnDate ,并且由于您在@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
);

But if you want to write it as verbose and redundant expressions then: 但是如果你想把它写成冗长和冗余的表达式,那么:

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;

Tough to verify though, because your existing query doesn't parse and you didn't show us sample data or desired results. 虽然很难验证,因为您现有的查询没有解析,并且您没有向我们展示样本数据或期望的结果。

I would avoid using the @Datetime + INT shorthand and would rather spell out the DATEADD operations, because the shorthand doesn't work with the newer types ( DATE , DATETIME2 , etc). 我会避免使用@Datetime + INT速记,而是拼出DATEADD操作,因为速记不适用于较新的类型( DATEDATETIME2等)。 I'd also avoid shorthand like mm as several of these abbreviations are non-obvious. 我也避免使用mm这样的速记,因为这些缩写中有几个是不明显的。 Three more characters makes that much more obvious. 另外三个字符使得更加明显。 Also the NOT ...something... IS NULL is not very intuitive to read. 还有NOT ...something... IS NULL读起来不是很直观。 I would rather say "the light is not red" than "not the light is red." 我宁愿说“光不是红”而不是“不是光是红的”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM