Instead of using recursive CTE to produce final date range I would use a different approach. CTE is limited to certain recursion limit. That means it will not generate date ranges longer than this limit (I think by default it is set to 100). In this case, it means you won't be able to create data ranges longer than 100 days.
My proposal is to use CTE to generate the so-called Tally Table and then use it with CROSS APPLY to get the desired result. That will enable you to create any amount of "unlimited" data ranges.
Let's create a table-valued function that will produce for us a range of integer numbers:
CREATE FUNCTION dbo.fnt_Numbers(@startValue int, @amount int)
RETURNS TABLE AS
RETURN
WITH lv0 AS (SELECT 0 g UNION ALL SELECT 0)
,lv1 AS (SELECT 0 g FROM lv0 a CROSS JOIN lv0 b) -- 4
,lv2 AS (SELECT 0 g FROM lv1 a CROSS JOIN lv1 b) -- 16
,lv3 AS (SELECT 0 g FROM lv2 a CROSS JOIN lv2 b) -- 256
,lv4 AS (SELECT 0 g FROM lv3 a CROSS JOIN lv3 b) -- 65,536
,Numbers AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS num FROM lv4)
SELECT @startValue + num - 1 AS n
FROM Numbers
WHERE num <= @amount;
You can use it in the following way:
SELECT n FROM dbo.fnt_Numbers(0, 10);
Example result:
Then you can use this code to generate the time ranges you need:
CREATE TABLE #Date (
StartDate date NOT NULL,
DaysAmount int NOT NULL
);
--Example data
INSERT INTO #Date (StartDate,DaysAmount)
VALUES ('2018-08-31', 10), ('2000-10-10', 5);
--Review table content
SELECT StartDate, DaysAmount
FROM #Date;
--Generate ranges
SELECT Dates.*
FROM #Date
CROSS APPLY (SELECT DATEADD(day,n, StartDate) AS [Date] , n FROM fnt_Numbers(0, DaysAmount)) AS Dates
ORDER BY StartDate, n;
Results:
select '2010-01-01' as start_date, '2010-01-01' as trial_date union all select start_date, dateadd(d, 1, trial_date) as trial_date from cte where trial_date <= '2010-01-10'
?