Oracle 的 ADD_MONTHS() 的 SQL Server 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3208042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:00:12  来源:igfitidea点击:

SQL Server version of Oracle's ADD_MONTHS()

sqlsql-serveroracledate-arithmetic

提问by Jacob Saylor

In Oracle, you can use ADD_Months to add months on the fly in the sql statement. What is the MS SQL version.

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

Oracle Example

甲骨文示例

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

Source : java's website

来源:java网站

回答by Alex K.

Its DATEADD(MONTH, TestFrequency, TestDate)to add TestFrequencynumber of months to the date field TestDate.

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

回答by OMG Ponies

SQL Server's TSQL equivalent to Oracle's PLSQL ADD_MONTHS functionis DATEADD:

SQL Server 的 TSQL 相当于Oracle 的 PLSQL ADD_MONTHS 函数DATEADD

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

回答by Charles Bretana

I'm not exactly sure how Oracles Add_Months works, but MS Sql has this:

我不太确定 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'

回答by Jersey_Guy

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

-- Please mark as answer if this helped you better then the answer before -

-- 如果这对您的帮助比之前的答案更好,请标记为答案-