在使用SQL Server函数计算截止日期时需要帮助

时间:2016-11-13 17:19:02

标签: sql-server sqldatetime

我试图通过以下约束使用发票日期计算到期日期

  • 将有两个文本框,一个是输入当月的日期,另一个是输入截止日期前一天

  • 如果月份的日期是4,而截止日期的前一天是5,我们必须通过减去4和5来计算值,如果减去的值是-ve 1并且输入的发票日期是11/13 / 2016所以截止日期必须是2017年4月12日(这里是04月份的日期)

  • 如果当月的日期为15,而截止日期为前一天为5且减去的值为10(非负数),则输入的发票日期为2016年11月13日,表示截止日期必须为12/15 / 2016(此处15是月中的某一天)如果发票日期小于或等于减去的值,即10,如发票日期11/09/2016,那么发票日期必须是2016年11月15日

请注意,在计算星期日时,上述约束也必须满足闰年

请帮我修新SQL服务器

我试过的代码

DECLARE  @INVOICEDATE DATETIME ='02/01/2016'
        ,@TENANTID BIGINT=29
        ,@PAYMENTTERMID BIGINT=2

BEGIN
    DECLARE @DUEDATE DATETIME
    DECLARE @ACTUALDUEDATE BIGINT
    DECLARE @CALCULATEDDATE DATETIME
    DECLARE @PAYMENTTYPES BIGINT
    DECLARE @DAYSOFMONTH BIGINT
    DECLARE @DAYSAFTERDUEDAY BIGINT
    DECLARE @NOOFDAYS BIGINT

    SELECT @PAYMENTTYPES = PAYMENT_TYPES
        ,@NOOFDAYS = NUMBER_OF_DAYS
        ,@DAYSOFMONTH = DAY_OF_MONTH
        ,@DAYSAFTERDUEDAY = DAYS_AFTER_DUE_DAY
        FROM XC_PAYMENT_TERMS_MASTER
        WHERE TENANT_ID = @TENANTID
        AND PAYMENT_TERM_ID = @PAYMENTTERMID 

    IF @PAYMENTTYPES = 1
    BEGIN
        SET @CALCULATEDDATE = DATEADD(dd, @NOOFDAYS, @INVOICEDATE)
    END
    ELSE IF @PAYMENTTYPES = 2
    BEGIN
        SET @ACTUALDUEDATE = @DAYSOFMONTH - @DAYSAFTERDUEDAY
            IF 1 = (
            IIF(DATEPART(dd, (
                        EOMONTH(CONCAT (
                                DATEPART(yyyy, @INVOICEDATE)
                                ,'0201'
                        ))
                )) = 29, 1, 0)
            )
            --LEAP YEAR
            BEGIN
            IF(@ACTUALDUEDATE <= 0)
            BEGIN
            IF DATEPART(dd, @INVOICEDATE) = 30
            AND  DATEPART(mm, @INVOICEDATE) =01
            BEGIN
            SET @CALCULATEDDATE=DATEADD(dd, 31, @INVOICEDATE)
            END
            ELSE IF DATEPART(dd, @INVOICEDATE) = 31
            AND  DATEPART(mm, @INVOICEDATE) =01
            BEGIN
            SET @CALCULATEDDATE=DATEADD(dd, 31, @INVOICEDATE)
            END
            ELSE
            BEGIN
            IF @DAYSOFMONTH = 30 OR @DAYSOFMONTH =31
            BEGIN
            SET @CALCULATEDDATE= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0, @INVOICEDATE)+2,0))
            END
            ELSE
            BEGIN
            DECLARE @NEWNEGATIVEDATE DATETIME
            SET @NEWNEGATIVEDATE = DATEADD(DAY, @DAYSOFMONTH - DATEPART(DAY, @INVOICEDATE), @INVOICEDATE);
            SET @DUEDATE = DATEADD(mm, 1, @NEWNEGATIVEDATE)
            END
            END
            END
            ELSE IF(@ACTUALDUEDATE > 0)
            BEGIN
            SELECT 2
            DECLARE @COMBINEDDATE DATETIME
            SET @COMBINEDDATE=DATEADD(DAY, @ACTUALDUEDATE - DATEPART(DAY, @INVOICEDATE), @INVOICEDATE);
            SELECT @INVOICEDATE AS INVDATE 
            SELECT @COMBINEDDATE AS COMBDATE
            IF @INVOICEDATE > @COMBINEDDATE
            BEGIN
            SELECT 115 AS TRUE
            DECLARE @NEWDATE DATETIME
            SET @NEWDATE = DATEADD(DAY, @DAYSOFMONTH - DATEPART(DAY, @INVOICEDATE), @INVOICEDATE);
            SET @DUEDATE = DATEADD(mm, 1, @NEWDATE)
            SELECT @DUEDATE AS DATES
            END

            ELSE 
            BEGIN
            SET @DUEDATE=DATEADD(DAY, @DAYSOFMONTH - DATEPART(DAY, @INVOICEDATE), @INVOICEDATE);
            SELECT @DUEDATE AS DATEDUE
            END

            END
            END
            ELSE 
            --NOT LEAP YEAR
            BEGIN

            END

        END
        END

1 个答案:

答案 0 :(得分:0)

最后通过创建以下功能解决了这个问题

BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
{
    Name = "myTask"
};
builder.SetTrigger(new ToastNotificationActionTrigger());
BackgroundTaskRegistration task = builder.Register();