0

My mind has gone totally blank this morning. I'm creating a proc and need it to pull results with a date-related WHERE clause. The WHERE clause should state that the report should look back two months from GetDate().

This is using T-SQL in SQL Server 2012. The column containing the date for the clause is called [Delivery Date].

Many thanks.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
Alec.
  • 5,371
  • 5
  • 34
  • 69

3 Answers3

2

Try this

SELECT * 
FROM tableName
WHERE [Delivery Date] < DATEADD(month, -2, GETDATE())

MSDN Link for DATEADD
Similar Question: Stackoverflow link

Community
  • 1
  • 1
asifsid88
  • 4,631
  • 20
  • 30
2

If [Delivery Date] has both date and time and want to consider time as well? then try

SELECT * 
FROM tableName
WHERE [Delivery Date] >= DATEADD(month, -2, GETDATE())

If [Delivery Date] is only a date or ignore the time part? then try

SELECT * 
FROM tableName
WHERE [Delivery Date] >= CONVERT(date, DATEADD(month, -2, GETDATE()))
Kaf
  • 33,101
  • 7
  • 58
  • 78
1

2 month ago starting the 1st of the month at 00:00

DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0)

2 month ago starting the same day of month (if applicable) at the same hour

DATEADD(mm, -2, GETDATE())
Serge
  • 6,554
  • 5
  • 30
  • 56