Search code examples
sqldatedatediffdateadd

SQL Retrieve past 6 months of data from table


I am looking to pull data for the past 6 months in "yesterday". I need the last 6 months excluding the current date. I have tried many different variations but always get the current date included.

Here is the SQL I have for the last 6 months:

([Production Date] >= DATEADD(Month, DATEDIFF(Month, 0, DATEADD(m, - 6, GETDATE())), 0))

Solution

  • you need an upper bound to exclude todays date.

    AND [Production Date] < Convert(date,GETDATE())