mw_sql_developer - Friday, March 23, 2018 3:03 PM
Here is a method that doesn't rely on string manipulation (slow) - and does not use a predefined Tally table (although - if you have one it would be better):
Declare @startDate date = '2017-01-10'
, @endDate date = '2018-12-15';
With monthlyIntervals (StartDate, EndDate)
As (
Select dateadd(day, 1, eomonth(@startDate, t.Number - 1))
, eomonth(@startDate, t.Number)
From (Select row_number() over(Order By ac.[object_id]) - 1 As Number From sys.all_columns ac) As t
Where t.Number <= datediff(month, @startDate, @endDate)
)
Select *
From monthlyIntervals;
This also uses the date data type - you really should not use strings to represent dates - that will cause all kinds of issues later on...
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs