SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


First and Last day of the month in a given date range


First and Last day of the month in a given date range

Author
Message
ArjunaReddy
ArjunaReddy
SSC Veteran
SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)

Group: General Forum Members
Points: 261 Visits: 554
Hi,

I would like to get the first and last day of any month in a given date range.

Ex: Display the first and last day of the months between @startDate and @EndDates.

Input Params= @StartDate='2016-06-21 16:57:11.093'
@EndDate = '2016-09-30 00:00:00.000'

OutPut should be:-

MonthStartDate MonthEndDate
1/06/2016 30/06/2016
1/07/2016 31/07/2016
1/08/2016 31/08/2016
1/09/2016 30/09/2016


Please help me with the query.

Thanks,
Nagarjun.
Dohsan
Dohsan
SSC Eights!
SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)

Group: General Forum Members
Points: 930 Visits: 3482
As a caveat this is pre-first morning tea, but should work.


DECLARE @StartDate DATETIME = '2016-06-21 16:57:11.093',
@EndDate DATETIME = '2016-09-30 00:00:00.000';


SELECT MonthStartDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) + N,0),
MonthEndDate = DATEADD(MONTH,DATEDIFF(MONTH,0,@StartDate)+ N + 1, 0)-1
FROM dbo.GetNums(0,DATEDIFF(MONTH,@StartDate,@EndDate));



dbo.GetNums is a numbers function that you should be able to find on this forum
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search