Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get automatically previous month date range in SQL?

Tags:

sql

sql-server

I have 2012 SQL Server stored procedure ran automatically every fifth of the month but the problem I have is supplying previous month date range e.g. I need to have clause like ...where datein between '2014-02-01' and '2014-02-28' and next month it would change it to ...where datein between '2014-03-01' and '2014-02-31' and so on.

thanks

like image 615
user4778 Avatar asked Jul 18 '14 19:07

user4778


1 Answers

This should work

SELECT DATEADD(DAY,1,EOMONTH(GETDATE(),-2)) AS StartDate, EOMONTH(GETDATE(),-1) AS EndDate

To be more specific in your WHERE clause

WHERE @datein BETWEEN DATEADD(DAY,1,EOMONTH(GETDATE(),-2)) AND EOMONTH(GETDATE(),-1)
like image 179
Dbloch Avatar answered Sep 30 '22 06:09

Dbloch