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

こ雲淡風輕ζ 提交于 2019-12-02 01:59:31

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
KM.

I wouldn't loop to create a list of dates, use a Numbers table (not just a table of values 0 to 9), they are usefull for many things: http://web.archive.org/web/20150411042510/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html With a true Numbers table you don't have to CROSS JOIN a bunch of times and make the query overly complex.

For this method to work, you need to do this one time table setup:

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.columns s1
    CROSS JOIN sys.columns s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

Once the Numbers table is set up, use this query:

SELECT
    @Start+Number-1
    FROM Numbers
    WHERE Number<=DATEDIFF(day,@Start,@End)+1

to capture them do:

DECLARE  @Start datetime
         ,@End  datetime
DECLARE @AllDates table
        (Date datetime)

SELECT @Start = '06/23/2008', @End = '06/30/2008'

INSERT INTO @AllDates
        (Date)
    SELECT
        @Start+Number-1
        FROM Numbers
        WHERE Number<=DATEDIFF(day,@Start,@End)+1

SELECT * FROM @AllDates

output:

Date
-----------------------
2008-06-23 00:00:00.000
2008-06-24 00:00:00.000
2008-06-25 00:00:00.000
2008-06-26 00:00:00.000
2008-06-27 00:00:00.000
2008-06-28 00:00:00.000
2008-06-29 00:00:00.000
2008-06-30 00:00:00.000

(8 row(s) affected)

One possible way (not saying it's the best or most efficient) would be something like this:

DECLARE @StartDate DATETIME
SET @StartDate = '06/23/2008'

DECLARE @EndDate DATETIME 
SET @EndDate = '06/30/2008'

DECLARE @TableOfDates TABLE(DateValue DATETIME)

DECLARE @CurrentDate DATETIME

SET @CurrentDate = @startDate

WHILE @CurrentDate <= @endDate
BEGIN
    INSERT INTO @TableOfDates(DateValue) VALUES (@CurrentDate)

    SET @CurrentDate = DATEADD(DAY, 1, @CurrentDate)
END

SELECT * FROM @TableOfDates

This will work with any number of dates, any range of dates, and doesn't need a specific "helper" table with integer values.

It stores all relevant dates into a in-memory table variable so you can then use it for e.g. another SELECT statement or whatever you need it for.

Marc

onedaywhen

See:

Why should I consider using an auxiliary calendar table?

A calendar table can make it much easier to develop solutions around any business model which involves dates. Last I checked, this encompasses pretty much any business model you can think of, to some degree. Constant problems that end up requiring verbose, complicated and inefficient methods include the following questions:

  • How many business days between x and y?
  • ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!