Search code examples
sql-server-2012ms-access-2010dateadd

How to convert Access statement to SQL to determine date range


I have an Access statement that I am trying to convert to be used in SQL Server 2012. Here is the statement:

>=DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [FiscalYear])) And <DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [FiscalYear]))+364

It is basically getting a rolling 12 month period. How can I use this in SQL or is there a better way to write this altogether?


Solution

  • Here's how you would get the past 12 months worth of data in SQL:

    SELECT *
    FROM Table
    WHERE DateField > DATEADD(MONTH, -12, GETDATE())