Expand date within date parameter

Hi,

I am working into a t-sql where i need to expand the date within a date parameter. For example:

Start Date = '1/1/2014'

End Date = '12/31/2014'

I want to expand this as:

01/2014

02/2014

03/2014

04/2014

05/2014

06/2014

up to 

12/2014

Please let me know if you need more information.

Thanks.

July 23rd, 2015 1:45am

Hi,

I am working into a t-sql where i need to expand the date within a date parameter. For example:

Start Date = '1/1/2014'

End Date = '12/31/2014'

I want to expand this as:

01/2014

02/2014

03/2014

04/2014

05/2014

06/2014

up to 

12/2014

Please let me know if you need more information.

Thanks.

or so basically, how do i get the months and year within specified date range....

btw, we are using sql server 2005.

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 2:37am

You may check the Calendar Table in Google, you will get umpteen examples.

Please find one of my google search result:

http://codeinet.blogspot.fr/2006/08/auxiliary-calendar-table-for-sql.html

 
July 23rd, 2015 2:52am

You can try this.

DECLARE @SDate DATETIME

DECLARE @TDate DATETIME

 

SET @SDate = '2014-1-01 00:00:00.000'

SET @TDate = '2014-12-31 00:00:00.000'

 

;WITH CTE AS

(

  SELECT @SDate Date

  UNION ALL

  SELECT DateAdd(month,1,Date) FROM CTE WHERE DateAdd(month,1,Date) <= @TDate

)

SELECT right(convert(varchar(10),Date,103),7) FROM CTE

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 2:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics