SQL Server version of Oracle's ADD_MONTHS()

﹥>﹥吖頭↗ 提交于 2019-12-24 16:15:59

问题


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

Oracle Example

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

Source : java's website


回答1:


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




回答2:


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

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



回答3:


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

   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'



回答4:


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 -



来源:https://stackoverflow.com/questions/3208042/sql-server-version-of-oracles-add-months

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!