﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / Select all months bewtween two dates / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 10:41:56 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>It'll depend on what he's looking for.  If he needs the start month, it needs a -1 on the row_number function, so that it starts with 0 instead of 1.  If he needs the names of the months, that'll need to be added to the final select.  If he needs it to give the first day of each month, that'll require a slight modification to the final select.  And so on.  Since I don't have those details, I set up a skeleton, and the OP can either flesh it out, or ask questions about the details, or (all to common) never speak up again and we won't know what was actually needed.</description><pubDate>Fri, 30 Jan 2009 07:27:47 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>[quote][b]GSquared (1/29/2009)[/b][hr]Ouch!  Yeah, that piece of code might work, but it hurts to look at![code];with Numbers (Number) as	(select row_number() over (order by object_id)	from sys.all_objects)select dateadd(month, number, @StartDate)from Numberswhere number &amp;lt;= datediff(month, @StartDate, @EndDate)[/code][/quote]Ahhhhh.... MUCH better.  No loops... no recurrsion in the CTE... nice tight code...The only thing that may be a problem is that (I believe... haven't tested the code) it looks like the day of the startdate and enddate are preserved instead of the whole month being included.  Guess it all depends on what the op actually needs.</description><pubDate>Thu, 29 Jan 2009 15:11:14 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>Good one SS!might want to modify it slightly to include starting month?;with Numbers (Number) as        (select row_number() over (order by object_id)        from sys.all_objects)select dateadd(month, number-1, @StartDate)from Numberswhere number-1&amp;lt;= datediff(month, @StartDate, @EndDate)</description><pubDate>Thu, 29 Jan 2009 15:00:28 GMT</pubDate><dc:creator>ganci.mark</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>The Numbers version can work across years, just have to have enough rows in the Numbers CTE (you can get quite a few with just sys.all_objects; if that's not enough, do a cross join).  The "I've declared ten-million variables" version can work across a single year, so far as I can tell.</description><pubDate>Thu, 29 Jan 2009 14:52:32 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>Do the dates always fall within the same year?orCan they be for example 10-20-2008 and 01-23-2009?</description><pubDate>Thu, 29 Jan 2009 14:46:52 GMT</pubDate><dc:creator>ganci.mark</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>Ouch!  Yeah, that piece of code might work, but it hurts to look at![code];with Numbers (Number) as	(select row_number() over (order by object_id)	from sys.all_objects)select dateadd(month, number, @StartDate)from Numberswhere number &amp;lt;= datediff(month, @StartDate, @EndDate)[/code]</description><pubDate>Thu, 29 Jan 2009 14:14:09 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>declare @month table(months varchar(30))declare @date1 datetimedeclare @date2 datetimeset @date1='10/1/2011'set @date2='9/1/2010'declare @year1 intdeclare @year2 intset @year1= year(@date1)set @year2= year(@date2)declare @month1 intdeclare @month2 intset @month1=month(@date1)set @month2=month(@date2)declare @count int declare @monthname datetimeif (@year1&amp;lt;&amp;gt;@year2)begin  if @month1=@month2  set @count=12 else if @month1&amp;lt;&amp;gt;@month2  set @count=12-ABS(@month1-@month2)+1if @date1&amp;lt;@date2 set @monthname=@date1else if @date2&amp;lt;@date1 set @monthname=@date2 while @count&amp;gt;0 begin    insert @month(months)    select datename(month,@monthname)    set @monthname=dateadd(mm,1,@monthname)  set @count=@count-1 endendif @year1=@year2beginif @month1=@month2  set @count=1else if @month1&amp;lt;&amp;gt;@month2 set @count=ABS(@month2-@month1)+1if @date1&amp;lt;@date2 set @monthname=@date1else if @date2&amp;lt;@date1 set @monthname=@date2 while @count&amp;gt;0 begin        insert @month(months)     select datename(month,@monthname)        set @monthname=dateadd(mm,1,@monthname)  set @count=@count-1 endendselect * from @month</description><pubDate>Thu, 29 Jan 2009 14:10:51 GMT</pubDate><dc:creator>Bijal Parekh</dc:creator></item><item><title>RE: Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>The best way to do that is to build a calendar table.The second best way is to use a numbers table.After that, it's a numbers CTE.After that, recursion.Are you in a position where you can build a calendar table?  It's just a table of dates, starting from whatever you need and ending whenever you need.  For example, a table of dates between 1 Jan 2000 and 31 Dec 2050, is a default that I use quite often.  You can then store in it things like which dates are holidays, weekends, etc.If you have a numbers table, you can do a query where you use DateAdd(month) on the numbers table, with the first date being what you add to.If you don't have either of those, and can't build either one, you might be able to build a numbers CTE, where you select row_number in a CTE, and then use that just like a numbers table.If none of those are possible, build a While loop and insert into a temp table (or a table variable if it needs to be a UDF), or build a recursive CTE with a Union All operator.</description><pubDate>Thu, 29 Jan 2009 13:33:24 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>Select all months bewtween two dates</title><link>http://www.sqlservercentral.com/Forums/Topic646320-149-1.aspx</link><description>Please help me in finding a query to get all months the between two dates.</description><pubDate>Thu, 29 Jan 2009 13:00:52 GMT</pubDate><dc:creator>aneel.c</dc:creator></item></channel></rss>