0

Is it possible to have a code that is similar to this logic:

DECLARE @LAST_MONTH
SET @LAST_MONTH = BETWEEN DATEADD(mm,-1,GETDATE()) AND DATEADD(mm,-2,GETDATE())

Except, this code doesn't work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

You need two variables... a startdate and an enddate

Using this post, here is how.

declare @startdate date = (select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)) --First day of previous month
declare @enddate date = (select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)) --Last Day of previous month

Then, use these in your where clause:

select...
from ...
where dateCol between @startdate and @enddate
S3S
  • 24,809
  • 5
  • 26
  • 45