2017-02-09 66 views
1

MS SQL Server的功能嘿我有一个函数试图将它从Oracle转换成MS SQL,但我得到附近的语法不正确“1”一行7:使用间隔

IF @nPaysQuarterly = 0 BEGIN 
    -- If the base date is less than March 1st of the given year then the due date is for that year 
    -- Else the due date is for the year plus 1 
     If @dBaseDate < convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), '')) BEGIN 
      SET @dDueDate = convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), '')); 
     END 
     ELSE BEGIN SET @dDueDate = convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), '')) + interval '1' year; 
     END 
    ELSE IF (@dBaseDate < @dAnnualDue1) BEGIN 
      SET @dDueDate = @dAnnualDue1; 
    ELSE IF (@dBaseDate < @dFirstQuarterDue) BEGIN 
      SET @dDueDate = @dFirstQuarterDue; 
    ELSE IF (@dBaseDate < @dSecondQuarterDue) BEGIN 
      SET @dDueDate = @dSecondQuarterDue; 
    ELSE IF (@dBaseDate < @dThirdQuarterDue) BEGIN 
      SET @dDueDate = @dThirdQuarterDue; 
    ELSE IF (@dBaseDate < @dAnnualDue) BEGIN 
      SET @dDueDate = @dAnnualDue; 
    END 

    RETURN AIP.GETNEXTBUSINESSDAY(@dDueDate) ; 
END; 

什么不对加1次递增时到今年?谢谢。

回答

1

我认为这是你正在寻找

IF (@nPaysQuarterly = 0) 
BEGIN 
    -- If the base date is less than March 1st of the given year then the due date is for that year 
    -- Else the due date is for the year plus 1 
     If (@dBaseDate < convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), ''))) 
      BEGIN 
       SET @dDueDate = convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), '')); 
      END 
     ELSE 
      BEGIN 
       SET @dDueDate = DATEADD(year,1,convert(DATETIME, '01-Mar-'+isnull(year(@dBaseDate), ''))); 
      END 

    IF (@dBaseDate < @dAnnualDue1) 
      SET @dDueDate = @dAnnualDue1; 
    ELSE IF (@dBaseDate < @dFirstQuarterDue) 
      SET @dDueDate = @dFirstQuarterDue; 
    ELSE IF (@dBaseDate < @dSecondQuarterDue) 
      SET @dDueDate = @dSecondQuarterDue; 
    ELSE IF (@dBaseDate < @dThirdQuarterDue) 
      SET @dDueDate = @dThirdQuarterDue; 
    ELSE IF (@dBaseDate < @dAnnualDue) 
      SET @dDueDate = @dAnnualDue; 
    END 

    RETURN AIP.GETNEXTBUSINESSDAY(@dDueDate) ; 
END; 

你必须使用DATEADD功能到一年添加到您的datetime值查询。另一个问题是,你不能用ELSE IF

+0

启动IF声明谢谢它去顺利,现在我在声明的开头另一条线日期皈依得到皈依错误: 'DECLARE @ dAnnualDue1 DATETIME = CONVERT( DATETIME,@nYear,'')+'/ 03/1';' 任何想法,这带来了什么? 错误表示:参数数据类型varchar对于转换函数的参数3无效。 –

+0

使用'convert(Datetime,Cast(@nYear as varchar(4)+'/ 03/01')'而不是它 – Hadi

+0

我使用它:'DECLARE @ dAnnualDue1 DATETIME = convert(Datetime,Cast(@nYear as varchar 4)+'/ 03/01');'但在'+'处得到错误语法, –