[英]MS SQL Server Function using Interval
Hey there I have a Function trying to convert it from Oracle to MS SQL but I get incorrect syntax near '1' one line 7:嘿,我有一个函数试图将它从 Oracle 转换为 MS SQL,但我在第 7 行的“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 = 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;
Anything wrong when adding 1 increment to the year ?向年份添加 1 个增量时有什么问题吗? Thank you.谢谢你。
i think this is the query you are looking for我认为这是您正在寻找的查询
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;
you have to use DATEADD
Function to add one year to your datetime value.您必须使用DATEADD
函数将一年添加到您的日期时间值。 another Issue is that you cannot start an IF
Statement with an ELSE IF
另一个问题是你不能用ELSE IF
开始一个IF
语句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.