tsql group by months with dateadd()

108 views Asked by At

I'm grouping data with dateadd() by weeks, but I want to group by months instead. I've got this fun little dateadd function.

   DATEADD(day, - (1 * DATEPART(dw, Orders.OrderDate - 1)) + 1, CONVERT (date, Orders.OrderDate, 103))

so we're subtracting a week off of the current date - it seemed to me as simple as just changing the datepart to month and subtracting 1 from it,however, this gives me some pretty wacky results how do I modify this to subtract months instead of weeks?

1

There are 1 answers

1
user1098532 On

so you basically already tried:

DATEADD(day, - (1 * MONTH(dw, Orders.OrderDate - 1)) + 1, CONVERT (date, Orders.OrderDate, 103))