开发者

SQL Server version of Oracle's ADD_MONTHS()

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 web开发者_如何学Pythonsite


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


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

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


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'


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 -

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜