I wanted to generate datetime
ranges in SQL Server 2000. I want something that will be compatible with 2000, 2005 and 2008. If its compatible with 2012, that would be great too.
So, I found the answer here - Generate Dates between date ranges . But, I want two columns instead of 1. I want a start date column and an end date column. How do I modify the query below to get it ?
-- Declarations
DECLARE @dates TABLE(dt datetime)
DECLARE @dateFrom datetime
DECLARE @dateTo datetime
SET @dateFrom = '2001/01/01'
SET @dateTo = '2001/01/12'
-- Query:
WHILE(@dateFrom < @dateTo)
BEGIN
SELECT @dateFrom = DATEADD(day, 1,@dateFrom)
INSERT INTO @dates
SELECT @dateFrom
END
-- Output
SELECT * FROM @dates
My code -
DECLARE @dates TABLE(StartDate datetime, EndDate datetime)
DECLARE @dateFrom datetime
DECLARE @dateTo datetime
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @dateFrom = '2001/01/01'
SET @dateTo = '2001/01/12'
WHILE(@dateFrom < @dateTo)
BEGIN
SELECT @dateFrom, @dateFrom = DATEADD(day, 1,@dateFrom)
INSERT INTO @dates
SELECT @dateFrom
END
SELECT * FROM @dates
Error - A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.