What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 345 |  Welcome, Guest!   Register  Login
 Home > Forums > Sql Server > List of Months & Days Between 2 Dates ...
Ravimakhija88

List of Months & Days Between 2 Dates

Replies: 13 | Posted by: Ravimakhija88 on 6/15/2012 | Category: Sql Server Forums | Views: 2067 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

CGN007
CGN007  
Posted on: 6/15/2012 6:15:42 AM
Level: Silver | Status: [Member] | Points: 25

Please try this query

DECLARE @date1 DATETIME, @date2 DATETIME, @lastdate DATETIME, @totalMonths INT,@totalDays INT,@counter INT

SET @date1 = '10/12/2011'
SET @date2 = '05/02/2012'

SET @totalMonths = DATEDIFF(m, @date1, @date2)
IF(@totalMonths < 0)
SELECT 'Second date parameter is prior to the first date parameter'
IF(@totalMonths =0)
SELECT DATENAME(month, @date1) [nameOfMonth],DATEDIFF (d,@date1,@date2) [NumberOfDays]
ELSE
BEGIN
CREATE TABLE #temp_months123
(
[nameOfMonth] VARCHAR(9),
[NumberOfDays] INT
)
SET @counter = 0

SELECT @lastdate= DATEADD( D, -1, DATEADD( mm, DATEDIFF( m, 0, @date1 ) + 1, 0 ) )+1;
WHILE @counter <@totalMonths
BEGIN
SELECT @totalDays=DATEDIFF (d,@date1,@lastdate)
INSERT INTO #temp_months123(nameOfMonth,NumberOfDays) VALUES (DATENAME(month, @date1),@totalDays)
SET @counter = @counter + 1
SET @date1=@lastdate
SET @lastdate = DATEADD(Month, 1, @date1)

END
SELECT @totalDays=DATEDIFF (d,@date1,@date2)+1
INSERT INTO #temp_months123 (nameOfMonth,NumberOfDays) VALUES (DATENAME(month, @date2),@totalDays)
SELECT * FROM #temp_months123
END

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

Pandians
Pandians  
Posted on: 6/15/2012 9:07:58 AM
Level: Silver | Status: [Member] [MVP] | Points: 25

Nice work!

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

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

Vedaraj
Vedaraj  
Posted on: 6/15/2012 9:55:52 AM
Level: Starter | Status: [Member] | Points: 25

Very good

vedaraj

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

CGN007
CGN007  
Posted on: 6/16/2012 2:58:37 AM
Level: Silver | Status: [Member] | Points: 25

@Pandians
Thanks man...

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

CGN007
CGN007  
Posted on: 6/16/2012 2:59:13 AM
Level: Silver | Status: [Member] | Points: 25

Thanks Vedaraj...

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

Pandians
Pandians  
Posted on: 6/16/2012 11:14:52 AM
Level: Silver | Status: [Member] [MVP] | Points: 25

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. | Reply | Alert Moderator 

CGN007
CGN007  
Posted on: 6/16/2012 11:33:15 AM
Level: Silver | Status: [Member] | Points: 25

I'll check this also and let u know the feedback...

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

CGN007
CGN007  
Posted on: 6/18/2012 2:45:59 AM
Level: Silver | Status: [Member] | Points: 25

@Pandians
Great...Its Working Fine..

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

CGN007
CGN007  
Posted on: 6/18/2012 4:36:22 AM
Level: Silver | Status: [Member] | Points: 25

@Pandians
Can you tell me which query is fast...?I mean the optimized one.

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

CGN007
CGN007  
Posted on: 6/23/2012 9:46:30 AM
Level: Silver | Status: [Member] | Points: 25

@Ravimakhija88
Mark as answer,if it helps ....

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

Kirthiga
Kirthiga  
Posted on: 6/25/2012 4:45:21 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

CGN007
CGN007  
Posted on: 7/31/2012 10:05:04 AM
Level: Silver | Status: [Member] | Points: 25

please mark it as answer....That helps other who search the same...

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

Praveenjha
Praveenjha  
Posted on: 4/28/2013 8:36:29 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 8:42:01 PM