In SQL, I am looking to extract data that is only applicable to the previous month. This I have done using the DATEPART function and it works fine where I check the 'M' in the DATEPART function to be -1 from the current Date. But I then have issues when I get to January as there is of course no month that is 1 less than 1.
Is there a way to avoid issues in SQL come January that I am not aware of or cannot think of?
My current syntax is as follows...
(DATEPART(M,p.renewal_date_key) = DATEPART(M,dateadd(M,-1,getdate()))
I then have a further DATEPART clause that does the same for the Year to ensure the Year value is the same as the current year.
Any thoughts on an alternative approach to avoid January issues would be a great help.
This is use in Microsoft as well by the way (so SQL Server and also used in a few historic Excel Power Querys)