Oracle的ADD_MONTHS()的SQL Server版本

时间:2010-07-08 21:11:30

标签: sql sql-server oracle date-arithmetic

在Oracle中,您可以使用ADD_Months在sql语句中动态添加月份。什么是MS SQL版本。

Oracle示例

Select TestDate, 
       TestFrequency,
        ADD_MONTHS(TestDate, TestFrequency) AS FutureTestDate 
  FROM Tests

来源:java's website

4 个答案:

答案 0 :(得分:6)

DATEADD(MONTH, TestFrequency, TestDate)TestFrequency个月数添加到日期字段TestDate

答案 1 :(得分:2)

SQL Server的等效于Oracle's PLSQL ADD_MONTHS function的TSQL为DATEADD

SELECT TestDate, 
       TestFrequency,
       DATEADD(mm, TestFrequency, TestDate)
  FROM TEST

答案 2 :(得分:1)

我不确定Oracles Add_Months是如何工作的,但是MS Sql有这个:

   Declare @NumMonthsToAdd TinyInt Set @NumMonthsToAdd  = 6
   Declare @aDate DateTime Set @aDate = '12 Jan 2010'
   Select DateAdd(month, @numMonthstoAdd, @aDate)
      -- above will generate datetime of '12 July 2010'

答案 3 :(得分:1)

CREATE FUNCTION [dbo].[ADD_MONTHS]
(
    @inDate SMALLDATETIME,
    @inFrequency INT

)
RETURNS DATETIME
AS
BEGIN
    RETURN DATEADD(MONTH, @inFrequency, @inDate)
END


-- TO Call : 
-- SELECT dbo.ADD_MONTHS(3,getdate()) AS newDate

- 如果这有助于你更好地回答之前的答案,请标记为答案 -