I need a query that filters from the first day of the previous month and the current day at 0 hours.
Can anyone help me?
Advertisement
Answer
Use date arithmetics:
where mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1))
Expression datefromparts(year(getdate()),month(getdate()), 1)
gives you the first day of the current month; you can then substract 1 month to get the result that you want.
If you want to filter out today’s data (and beyond), then:
where mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1)) and mydate < cast(getdate() as date)