Return records of last 6 months
Below is my SQL statement
DECLARE @dStart datetime , @dEnd datetime SET @dEnd = GETDATE() SET @dStart = DATEADD(mm, -6, @dEnd) Select * from MyTable Where TheDate Between @dStart AND @dEnd
This will return all the records from today minus 6 months data.
But I want this months data plus only the previous 5 months data.
Currently it will return records from March as well.
DATEADD(mm, -6, @dEnd)
You might use
dateadd(month, datediff(month, 0, @dEnd) - 5, 0)
This will truncate date to first of current month and substract five months from it.