Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Scott
Posting Yak Master
145 Posts |
Posted - 2002-01-21 : 16:59:26
|
| Is there a way to list the months (Numbers), between a start and an end date? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-01-21 : 17:07:10
|
| What do you mean?Are the dates always in the same year?select m.ifrom(select i = 1 union select 2 union select 3 union .... union select 12) as mwhere m.i between datepart(mm,@start) and datepart(mm,@end)You could put all the numbers in a temp table if you wish.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
Scott
Posting Yak Master
145 Posts |
Posted - 2002-01-22 : 01:16:42
|
| eg: Start = 20011001 End = 20010530resultmonth | Year-------------10 | 200111 | 200112 | 20011 | 20022 | 20023 | 20024 | 20025 | 2002 |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-01-22 : 01:23:02
|
| easiest way probablycreate table #a (d datetime)select @start = convert(varchar(6),@start,112) + '01'while @start < = @endbegininsert #a select @startdateadd(mm,1,@start)endselect datepart(mm,d), datepart(yy,d)from #aorder by ddrop table #a==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-01-22 : 01:33:59
|
yep and scott, it'll work even better if your start date is earlier than your end date I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
Scott
Posting Yak Master
145 Posts |
Posted - 2002-01-22 : 01:44:13
|
| Thanks, one small syntax error:declare @Start smalldatetimedeclare @End smalldatetimeselect @Start = '20010101'select @End = '20021001'set dateformat dmycreate table #a (d datetime)While @start < = @endbegininsert #a select @startselect @start = dateadd(mm,1,@start)endselect datepart(mm,d), datepart(yy,d)from #aorder by ddrop table #aEdited by - scott on 01/22/2002 01:58:39 |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-01-22 : 01:48:48
|
| That's why I always put the increment at the beginning of the loop - except this time to proove it's a good idea.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|