Don't have usable test data, but I'm almost certain this is it:
;WITH
cteTally10 AS (
SELECT * FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS numbers(number)
),
cteTally100 AS (
SELECT 0 AS number
FROM cteTally10 c1
CROSS JOIN cteTally10 c2
),
cteTally10K AS (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS number
FROM cteTally100 c1
CROSS JOIN cteTally100 c2
)
SELECT DATEADD(MONTH, ct.number, start_date) AS dates
FROM dbo.main_table mt
INNER JOIN cteTally10K t ON ct.number BETWEEN 0 AND DATEDIFF(MONTH, start_date, end_date)
I have a data set of nearly 500K rows. the name of the table is 'sample_set' and the columns are as follows id, startdate__c, enddate__C. could you please help me incorporate the informaiton in to your solution.
;WITH cteTally10 AS ( SELECT * FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS numbers(number) ), cteTally100 AS ( SELECT 0 AS number FROM cteTally10 c1 CROSS JOIN cteTally10 c2 ), cteTally10K AS ( SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS number FROM cteTally100 c1 CROSS JOIN cteTally100 c2 )
SELECT ss.id, DATEADD(MONTH, ct.number, ss.startdate__c) AS enddate
FROM sample_set ss INNER JOIN cteTally10K t ON ct.number BETWEEN 0 AND DATEDIFF(MONTH, startdate__c, enddate__c)