Search code examples
sqlsql-servert-sqlsql-server-2000gaps-and-islands

How to display all the dates between two given dates in SQL


Using SQL server 2000. If the Start date is 06/23/2008 and End date is 06/30/2008

Then I need the Output of query as

06/23/2008
06/24/2008
06/25/2008
.
.
.
06/30/2008

I Created a Table names as Integer which has 1 Column, column values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 then I used the below mentioned query

Tried Query

SELECT DATEADD(d, H.i * 100 + T .i * 10 + U.i, '" & dtpfrom.Value & "') AS Dates 
  FROM integers H 
CROSS JOIN integers T 
CROSS JOIN integers U 
order by dates

The above query is displaying 999 Dates only. 999 Dates means (365 + 365 + 269) Dates Only. Suppose I want to select more than 3 Years (01/01/2003 to 01/01/2008). The above query should not suitable.

How to modify my query? Or any other query is available for the above condition.

Please kindly provide me the Query.


Solution

  • This will get you up to 100,000 days:

    SELECT DATEADD(d, Y.i * 10000 + X.i * 1000 + H.i * 100 + T .i * 10 + U.i, '" & dtpfrom.Value & "') AS Dates 
    FROM integers H 
    CROSS JOIN integers T 
    CROSS JOIN integers U 
    CROSS JOIN integers X 
    CROSS JOIN integers Y 
    order by dates