SQL Date Help

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hi,

Is there a way in T-SQL to return all of the months between two given dates?

For example, if the two dates were 01/01/2010 and 01/03/2010, it would return Jan, Feb, Mar.

Can this be done? I'm using SQL Server 2005 if that makes any difference?

Thanks,
 
Thanks Pho - that worked great!

Next challenge is;

I have another table containing a reference and the two dates used above.

How would I adapt the above, so that the output would be:

Ref , Month
REF001, Jan
REF001, Feb
REF001, Mar
REF002, May
REF002, Jun
REF003, Mar
REF003, Apr
REF003, May
etc.

I'm assuming I need another WHILE loop around the above, to loop through each reference and doing the date calculation before moving on to the next reference. How would I tell it to move on to the next reference? (The above example is for illustration purposes only - each ref is move complex than that and not necessarily sequential.)

Can you help?

Thanks,
 
Last edited:
Pho that looks like just what I'm after - I'll give it a go tomorrow when I'm back in the office. Never heard of CROSS APPLY before.
 
Back
Top Bottom