# SQL Server: Divide Date Range into Months

I wrote a script the other day that is utterly useless to me, but it might be helpful for someone else, so I thought I’d write a quick post about it.

Suppose you have some date range, let’s say “1/20/2013 - 4/29/2014”. Now, suppose you want this range to be broken up into one month sections, with a start and end date for each month. Suppose, for some reason, you want to use a recursive common table expression (CTE). You can just do this:

DECLARE @BeginDate DATETIME = '2013-01-20'
DECLARE @EndDate DATETIME = '2014-04-29'

;WITH Months (currentDate)
AS ( SELECT @BeginDate
UNION ALL