Try to use
CTE[
^] then join data returned by recursive query with your data:
;WITH AllDates AS
(
SELECT CONVERT(DATE, '2015-10-20') AS Dates
UNION ALL
SELECT DATEADD(DD, 1, Dates) AS Dates
FROM AllDates
WHERE DATEADD(DD, 1, Dates)< '2015-10-28'
)
SELECT t1.Dates, COALESCE(t2.Contacted, 0) AS Contacted
FROM AllDates As t1 LEFT JOIN YourTableName AS t2 ON t1.Dates = t2.Dates
For further information, please see:
WITH common_table_expression (Transact-SQL)[
^]
Recursive Queries Using Common Table Expressions[
^]
DATEADD[
^]
Visual Representation of SQL Joins[
^]
Complete sample:
SET DATEFORMAT ymd;
DECLARE @YourTableName TABLE(dates DATE, contacted INT)
INSERT INTO @YourTableName (dates, contacted)
VALUES('2015-10-20', 3),
('2015-10-21', 1),
('2015-10-22', 5),
('2015-10-24', 10),
('2015-10-27', 3)
DECLARE @startDate DATE = '2015-10-20'
DECLARE @endDate DATE = '2015-10-28'
;WITH AllDates AS
(
SELECT @startDate AS Dates
UNION ALL
SELECT DATEADD(DD, 1, Dates) AS Dates
FROM AllDates
WHERE DATEADD(DD, 1, Dates)<= @endDate
)
SELECT t1.Dates, COALESCE(t2.Contacted, 0) AS Contacted
FROM AllDates As t1 LEFT JOIN @YourTableName AS t2 ON t1.Dates = t2.Dates
Returns:
Dates Contacted
2015-10-20 3
2015-10-21 1
2015-10-22 5
2015-10-23 0
2015-10-24 10
2015-10-25 0
2015-10-26 0
2015-10-27 3
2015-10-28 0