Click here to Skip to main content
15,920,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to display all the dates starting from '1/1/2000' to '1/12/2020' in sql or ms access.
Posted

SQL
DECLARE @startDate DATETIME
    DECLARE @endDate DATETIME

    SET @startDate = '2013-01-01'
    SET @endDate = '2013-01-31';

    WITH dates(Date) AS 
    (
        SELECT @startdate as Date
        UNION ALL
        SELECT DATEADD(d,1,[Date])
        FROM dates 
        WHERE DATE < @enddate
    )

    SELECT Date
    FROM dates
    OPTION (MAXRECURSION 0 )
    GO
 
Share this answer
 
v2
Comments
Maciej Los 17-Mar-13 13:52pm    
Nice CTE function ;)
+5!
Hi,

you can make use of DATEADD function of SQL.

refer below links for more on DATEADD.
DATEADD-MSDN[^]
SQL Server DATEADD() Function[^]

So, try like below.

SQL
DECLARE @StartDate DATETIME, @EndDate DATETIME
CREATE TABLE @Dates (DateCol DATETIME)

SET @StartDate = '1/1/2000'
SET @EndDate = '1/12/2020'

INSERT INTO @Dates Values(@StartDate)

WHILE @StartDate < @EndDate
BEGIN
    SET @StartDate = DATEADD(day,1,@StartDate)
    INSERT INTO @Dates Values(@StartDate)
END
INSERT INTO @Dates Values(@EndDate)

SELECT DateCol FROM @Dates


hope it helps.
 
Share this answer
 
Comments
Maciej Los 17-Mar-13 13:51pm    
+5!
Karthik Harve 18-Mar-13 0:07am    
Thanks Los. !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900