Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
SELECT DISTINCT startdate FROM Orders
WHERE startdate BETWEEN '11/1/2002' AND '12/31/2002'
UNION
SELECT DISTINCT enddate FROM Orders
WHERE enddate BETWEEN '11/1/2002' AND '12/31/2002'
SET NOCOUNT ON
CREATE TABLE #WorkDays
(OrderID int NOT NULL, WorkDate datetime NOT NULL)
--Put starting and ending dates in
INSERT #WorkDays (OrderID, WorkDate)
SELECT OrderID, Startdate FROM #Orders
UNION
SELECT OrderID, Enddate FROM #Orders
--Insert missing dates
--as long as dates were just inserted
WHILE @@ROWCOUNT > 0
INSERT #WorkDays (OrderID, WorkDate)
SELECT w2.OrderID, DateAdd(day,1,w2.WorkDate)
FROM #WorkDays w2
JOIN Orders o ON o.OrderID=w2.OrderID
WHERE DateAdd(day,1,w2.WorkDate) NOT IN
(SELECT WorkDate FROM #WorkDays w1
WHERE w1.OrderID = w2.OrderID)
AND DateAdd(day,1,w2.WorkDate) < o.EndDate
--Output the data and clean up
SELECT * FROM #WorkDays ORDER BY OrderID, WorkDate
DROP TABLE #WorkDays