Получение дат между диапазонами дат - PullRequest
16 голосов
/ 07 ноября 2008

Мне нужно получить все даты в диапазоне дат, используя SQL Server 2005

Ответы [ 7 ]

57 голосов
/ 07 ноября 2008

Вот, пожалуйста:

DECLARE @DateFrom smalldatetime, @DateTo smalldatetime;
SET @DateFrom='20000101';
SET @DateTo='20081231';
-------------------------------
WITH T(date)
AS
( 
SELECT @DateFrom 
UNION ALL
SELECT DateAdd(day,1,T.date) FROM T WHERE T.date < @DateTo
)
SELECT date FROM T OPTION (MAXRECURSION 32767);
8 голосов
/ 07 ноября 2008

Если у вас есть даты в таблице и вы просто хотите выбрать их между двумя датами, вы можете использовать

select * from yourTable where yourDate between date1 and date2

Если вы хотите создать даты из ничего, вы можете сделать это с помощью цикла или заполнить временную таблицу датами, а затем выбрать из этого.

4 голосов
/ 04 декабря 2016
DECLARE @Date1 DATE='2016-12-21', @Date2 DATE='2016-12-25'
SELECT DATEADD(DAY,number,@Date1) [Date] FROM master..spt_values WHERE type = 'P' AND DATEADD(DAY,number,@Date1) <= @Date2
1 голос
/ 13 августа 2009

Чуть более сложным, но, возможно, более гибким было бы использование таблицы, содержащей последовательный набор чисел. Это позволяет использовать более одного диапазона дат с разными интервалами.

/* holds a sequential set of number ie 0 to max */
/* where max is the total number of rows expected */
declare @Numbers table ( Number int  )

declare @max int 
declare @cnt int

set @cnt = 0
/* this value could be limited if you knew the total rows expected */
set @max = 999 

/* we are building the NUMBERS table on the fly */
/* but this could be a proper table in the database */
/* created at the point of first deployment */
while (@cnt <= @max)
begin
      insert into @Numbers select @cnt
      set @cnt = @cnt + 1
end

/* EXAMPLE of creating dates with different intervals */

declare @DateRanges table ( 
   StartDateTime datetime, EndDateTime datetime, Interval int )

/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009', '10 Jan 2009', 1 /* 1 day interval */
union select '01 Feb 2009', '10 Feb 2009', 2 /* 2 day interval */

/* heres the important bit generate the dates */
select
      StartDateTime
from
(
      select
            d.StartDateTime as RangeStart,
            d.EndDateTime as RangeEnd,
            dateadd(DAY, d.Interval * n.Number, d.StartDateTime) as StartDateTime
      from 
            @DateRanges d, @Numbers n
) as dates
where
      StartDateTime between RangeStart and RangeEnd
order by StartDateTime

Я использую вариацию этого, чтобы разбить даты на временные интервалы (с различными интервалами, но обычно длиной 5 минут). Моя таблица @numbers содержит максимум 288, так как это общее количество 5-минутных слотов, которое вы можете иметь за 24 часа.

/* EXAMPLE of creating times with different intervals */

delete from @DateRanges 

/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009 09:00:00', '01 Jan 2009 12:00:00', 30 /* 30 minutes interval */
union select '02 Feb 2009 09:00:00', '02 Feb 2009 10:00:00', 5 /* 5 minutes interval */

/* heres the import bit generate the times */
select
      StartDateTime,
      EndDateTime
from
(
      select
            d.StartDateTime as RangeStart,
            d.EndDateTime as RangeEnd,
            dateadd(MINUTE, d.Interval * n.Number, d.StartDateTime) as StartDateTime,
            dateadd(MINUTE, d.Interval * (n.Number + 1) , StartDateTime) as EndDateTime
      from 
            @DateRanges d, @Numbers n
) as dates
where
      StartDateTime >= RangeStart and EndDateTime <= RangeEnd
order by StartDateTime
0 голосов
/ 07 ноября 2008

Вот версия Oracle для генерации даты:

SELECT TO_DATE ('01-OCT-2008') + ROWNUM - 1 g_date
  FROM all_objects
 WHERE ROWNUM <= 15

вместо all_objects это может быть любая таблица с достаточным количеством строк для покрытия требуемого диапазона.

0 голосов
/ 07 ноября 2008

Если вы хотите, чтобы все даты присутствовали в вашей базе данных между двумя датами (то есть, какие даты заказчики разместили в 3 квартале 2008 года), вы должны написать что-то вроде этого:

select distinct(orderPlacedDate) 
from orders 
where orderPlacedDate between '2008-07-01' and 2008-09-30' 
order by orderPlacedDate
0 голосов
/ 07 ноября 2008

Чтобы сгенерировать диапазон дат, вы можете написать табличную функцию. Это функция, которая создает измерение даты для хранилища данных - вы, вероятно, можете довольно легко адаптировать его, обрезав специальные.

Редактировать: здесь без иерархии измерения даты.

if object_id ('ods.uf_DateHierarchy') is not null
    drop function ods.uf_DateHierarchy
go

create function ods.uf_DateHierarchy (
       @DateFrom datetime
      ,@DateTo   datetime
) returns @DateHierarchy table (
        DateKey           datetime
) as begin
    declare @today           datetime  
    set @today = @Datefrom

    while @today <= @DateTo begin
        insert @DateHierarchy (DateKey) values (@today)
        set @today = dateadd (dd, 1, @today)
    end

    return
end

go
...