mouse
mouse

Reputation: 141

SQL Server 2008 Generate a Series of date times

I am trying to generate a table with a series of date times in it.

I have the specified start date time and end date time(end date time is end of sequence), I add a time interval ~ (this can vary) to the start date time in seconds and this gives me the end date time.

The next sequence uses the end date time as its start value and adds the time interval in seconds to it. To demonstrate output I need. Is there a quick way to create such a table, other than using a lot of insert into commands?, I am really stumped

StartTime               Endtime                 Duration
2011-07-20 11:00:33     2011-07-20 11:09:47     554
2011-07-20 11:09:47     2011-07-20 11:19:01     554

    declare @StartTime datetime = '2011-07-20 11:00:33',
    @EndTime datetime = '2011-07-20 15:37:34'
    @Interval int = 554 -- this can be changed.

    insert into tmp_IRange
    values('2011-07-20 11:00:33', DATEADD(SECONDS, @Duration, 2011-07-20 11:00:33))

this becomes very tedious.. especially when the end date time is 2011-07-20 15:37:34 there are a lot of insert statements to make :(

Upvotes: 14

Views: 16078

Answers (6)

gbn
gbn

Reputation: 432210

Use a recursive CTE

declare @StartTime datetime = '2011-07-20 11:00:33',
    @EndTime datetime = '2011-07-20 15:37:34',
    @Interval int = 554 -- this can be changed.

;WITH cSequence AS
(
    SELECT
       @StartTime AS StartRange, 
       DATEADD(SECOND, @Interval, @StartTime) AS EndRange
    UNION ALL
    SELECT
      EndRange, 
      DATEADD(SECOND, @Interval, EndRange)
    FROM cSequence 
    WHERE DATEADD(SECOND, @Interval, EndRange) < @EndTime
)
 /* insert into tmp_IRange */
SELECT * FROM cSequence OPTION (MAXRECURSION 0);

Upvotes: 19

Andriy M
Andriy M

Reputation: 77657

Here's another non-recursive set-based solution, which uses a system table called master..spt_values:

DECLARE
  @StartTime datetime = '2011-07-20 11:00:33',
  @EndTime datetime = '2011-07-20 15:37:34',
  @Interval int = 554;
SELECT
  StartTime = DATEADD(SECOND, (number - 1) * @Interval, @StartTime),
  EndTime   = DATEADD(SECOND, (number - 0) * @Interval, @StartTime),
  Duration  = @Interval
FROM master..spt_values
WHERE type = 'P'
  AND number BETWEEN 1 AND DATEDIFF(SECOND, @StartTime, @Endtime) / @Interval

UNION ALL

SELECT
  DATEADD(SECOND, -Duration, EndTime),
  EndTime,
  Duration
FROM (
  SELECT
    EndTime = @EndTime,
    Duration = DATEDIFF(SECOND, @StartTime, @Endtime) % @Interval
) s
WHERE Duration > 0

The first SELECT generates a row set consisting of short intervals of specified length that lie within the specified range. If necessary, the second SELECT adds an interval between the first SELECT's last interval's end time and the specified end time.

The subset of master..spt_values that is particularly used here (and can be used in many similar cases) provides a list of numbers from 0 to 2047. This means for you that you will not be able to use this solution with that table if the initial interval is going to be split into more that 2047 short(er) intervals. You should then think of something like your own number table.

Upvotes: 4

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

This one will give the individual ranges but will ignore your actual end time (since it is < @interval after the last valid range):

;WITH x AS 
(
    SELECT TOP (DATEDIFF(SECOND, @StartTime, @EndTime)/@Interval) 
        rn = ROW_NUMBER() OVER (ORDER BY [object_id])
    FROM sys.objects
)
-- INSERT INTO dbo.tmp_IRange
SELECT DATEADD(SECOND, @Interval * (rn-1), @StartTime),
    DATEADD(SECOND, @Interval * rn, @StartTime)
FROM x;

Upvotes: 6

Max
Max

Reputation: 804

please try this code:

create table #T (date_begin datetime, date_end datetime)    

declare @StartTime datetime = '2011-07-20 11:00:33',
    @EndTime datetime = '2011-07-20 15:37:34',
    @Interval int = 554 -- this can be changed.

while DATEADD(ss,@Interval,@StartTime)<=@EndTime
begin
    insert #T
    select @StartTime, DATEADD(ss,@Interval,@StartTime)

    set @StartTime = DATEADD(ss,@Interval,@StartTime)
end


select * from #T

Upvotes: 0

teenboy
teenboy

Reputation: 368

Hope this helps...

declare @StartTime datetime = '2011-07-20 11:00:33',
@EndTime datetime = '2011-07-20 11:00:33',
@Interval int = 554,

@LimitTime datetime = '2011-07-20 15:37:34'

WHILE @EndTime < @LimitTime
BEGIN
SELECT @EndTime = DATEADD(S, @Interval, @StartTime)

SELECT @StartTime, @EndTime
--INSERT INTO tmp_IRange VALUES(@StartTime, @EndTime)

SELECT @StartTime = @EndTime

END

Upvotes: 0

Yuck
Yuck

Reputation: 50825

This should get you started. You can adapt it to your specific needs. As written it will generate a row for each minute increment starting with the current date & time.

DECLARE @BaseDate DateTime = GETDATE();

WITH DateTable (DateValue) AS (
    SELECT @BaseDate DateValue
    UNION ALL
    SELECT DATEADD(Minute, 1, DateValue) DateValue
    FROM DateTable
)
SELECT *
FROM DateTable
WHERE DateValue < DATEADD(Day, 1, GETDATE())
OPTION (MAXRECURSION 0);

Upvotes: 4

Related Questions