Saturday, August 6, 2011

Writing a recursive query to find out all dates between two given date

--Declaring the required variables DECLARE @fromdate DATETIME DECLARE @todate DATETIME SET @fromdate = '2010-01-01' SET @todate = '2010-01-10' --Query to fetch all the dates between two given date ;WITH tmpinfo(tmpdt) AS (SELECT @fromdate UNION ALL SELECT tmpdt + 1 FROM tmpinfo WHERE tmpdt < @todate) SELECT * FROM tmpinfo OPTION (MAXRECURSION 0);

No comments: