question

Bill Howard avatar image
Bill Howard asked

Number of months between two dates

I need query using sql server 2000 that will return me a record for each date between 2 dates for example if i passed 08/13/2008 as the beginning date and 08/13/2009 it would return 12 records i.e. 08/13/2008 09/13/2008 10/13/2008 11/13/2008 12/13/2008 01/13/2009 thru 08/13/2009

I would possibly in a stored procedure I'm in the Insurance Business and i need to make some computationson each record returned i.e. earned income

sql-server-2000
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
TG avatar image
TG answered

This could use any numbers table, tally table, or function that has/returns contiguous integers.

declare @startDate datetime            
        ,@endDate datetime            
            
select @startDate = '2008-10-13'            
       ,@endDate = '2009-10-13'            
            
select dateadd(month, number, @startDate) as YourNewDate            
from   master..spt_values as anyNumbersTable            
where  type = 'P'            
and    number < datediff(month, @startDate, @endDate)            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.