Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I am facing a serious issue and needs a solution.

I am having 2 dates say 15 April 2015 to 10 June 2015 and i want to get all the monthly breakups possible in between.

Like on entering two above dates i should get in return like
15 April - 30 April
1 May - 31 May
1 June - 10 June


Please Advice.
Posted
Comments
OriginalGriff 11-Sep-15 4:25am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps some samples of your database content and what you expect to get would help?
Use the "Improve question" widget to edit your question and provide better information.
Schatak 11-Sep-15 4:27am    
Agree. !
Vinay1337 11-Sep-15 4:28am    
Have clearly mentioned the input and expected output what else you need.please tell.
Schatak 11-Sep-15 4:28am    
What you have tried? Did you try to google it? i tired, and find so many examples. pick any of then try and if you stuck please post here.
Thanks
Vinay1337 11-Sep-15 4:43am    
can you provide me some links it would be helpful

i think this is what you are expecting as output:
Following query can be more simplyfy, you can do it with two date variables as well.
SQL
declare @StrtDt     smalldatetime
declare @EndDt      smalldatetime
DECLARE @WkStartDt  smalldatetime
DECLARE @WkEndDt    smalldatetime

DECLARE @Output TABLE (StartDate datetime, Enddate datetime )

set @EndDt =  '2015-06-10'
SET @WkStartDt = '2015-04-15'
WHILE @WkStartDt <= @EndDt
BEGIN
    SET @WkEndDt = dateadd(day, 0 - day(dateadd(month, 1 , @WkStartDt)), dateadd(month, 1 , @WkStartDt))

    INSERT INTO @Output(startdate,enddate)
        VALUES ( @WkStartDt, @WkEndDt)

    SET @WkStartDt = DATEADD(day, 1, @WkEndDt)
End
select   * from @Output

i hope this will help you
Thanks
 
Share this answer
 
With a loop between startDate.Month and endDate.Month you should be able to produce the solution.
 
Share this answer
 
Comments
/\jmot 12-Sep-15 11:35am    
this should be a comment, or you really need to update your answer with with little bit description.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900