List of Months & Days Between 2 Dates

Posted by Ravimakhija88 under Sql Server on 6/15/2012 | Points: 10 | Views : 7817 | Status : [Member] | Replies : 5
Hi All,

I want to find list of months & days between 2 dates.

Eg.From Date - 10/12/2011 To Date - 05/02/2012

Expected Output (that i need):

Months    Days
------- -----
Dec 21
Jan 31
Feb 05


Thanks




Responses

Posted by: Pandians on: 6/15/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Nice work!

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Ravimakhija88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vedaraj on: 6/15/2012 [Member] Starter | Points: 25

Up
0
Down
Very good

vedaraj

Ravimakhija88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pandians on: 6/16/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Do you want another simple way ? Without Loop ?

Try this!
Declare @Start Date,@End Date

Select @Start = '2011/12/10', @End='2012/02/05'

;With CTE
As
(
Select @Start [Date]
Union All
Select DateAdd(Day,1,[Date]) [Date] From CTE Where [Date] <=DateAdd(Day,-1,@End)
),Final
As
(
Select Cast(Cast(Year([Date]) as varchar) + '-' + datename(Month,[Date]) + Cast('-01' as varchar) as date) [Seq], Cast(datename(Month,[Date]) as varchar(3)) [Months],Count(1) [Counts] from CTE
Group By datename(Month,[Date]), Cast(Year([Date]) as varchar) + '-' + datename(Month,[Date]) + Cast('-01' as varchar)
)

Select Months, Counts from Final Order by Seq
Go
Result

Months Counts
------ ------
Dec 22
Jan 31
Feb 5
Note:
Use MAXRECURSION when the recursion exceeds 100! (Default is 100 !)

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Ravimakhija88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kirthiga on: 6/25/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try this below query it will help you

Declare @StartDay datetime, @EndDay datetime

Set @StartDay='2011-12-10'
Set @EndDay='2012-02-05'

;with cte(Date) as
(
select @StartDay
union all
select Date+1 from cte where Date < @EndDay
)
select DATENAME(M,Date)Months,count(Date)Days from cte group by DATENAME(M,Date)


Ravimakhija88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Praveenjha on: 4/28/2013 [Member] Starter | Points: 25

Up
0
Down
Hi CGN007 and Pandians,

Your reply helped me a lot. Thank you so much.

But i get to implement a new scenario.
Instead of count of days, i need to count the working days i.e., need to exclude Saturday and Sunday while counting days.

can you guys help me out. I am really stuck at this point. i need to implement this scenario within 2 days.

hope for ur reply asap.

Thanx & Regards,

Praveen

Ravimakhija88, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response