I am creating a Stored Procedure to get all the records from the previous 4 months, given the month and year variables.
The datetime format of etimestamp
column is 12/30/2020 12:000:00 AM.
If inMonth >= 4, this statement will work:
SELECT * FROM table_name
WHERE Month(etimestamp) in (inMonth , inMonth - 1, inMonth - 2, inMonth - 3) AND
Year(etimestamp) = inYear
However, this won't work if inMonth is not >= 4.
I want a query that will also work for the last year.
For example: inMonth = 2 AND inYear = 2020
Expected Output Records:
- February 2020
- January 2020
- December 2019
- November 2019