Relative Month Calculation

  • Is there a way in SQL 2005 (a function would be great but I doubt it) to calculate the relative month i.e. current month is zero, previous -1, etc, without having to write the code by long hand?

    The issue becomes more complicated once you step over the 12 month threshold, otherwise it would just have been a difference between the dates such as

    MONTH(GETDATE())-MONTH(date_value) AS relative_month

    Thx for your help

  • SELECT DATEADD(month, -1, GETDATE())

    DECLARE @NoPrevious INT

    SET @NoPreviou = 13

    SELECT DATEADD(month, -@NoPrevious, GETDATE())

  • select datediff(month,getdate(),'01jan2000')



    Clear Sky SQL
    My Blog[/url]

  • hmmmm....

    simple and effective....where is my brain this morning :blush:

    Thx for the code..

  • haha.. happens sometimes.. happened to me too...

    Happy coding

    -RP

    -RP
  • Combine brain failure and working with Oracle in place of SQL Server for the last few months... and bingo!

    Forget all the functions and the most simple of methods.. 😛

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply