How to generate minute intervals between two dates in T-SQL?

后端 未结 3 905
一个人的身影
一个人的身影 2020-12-20 10:00

I have a table of startTime and endTimes. I need to generate a table of intervals between those two dates in minutes. Here\'s some sample data:

declare @inte         


        
相关标签:
3条回答
  • 2020-12-20 10:33

    A numbers table can solve your problem. Assuming you don't need more than a few thousand rows, then this should work:

    with n as (
          select row_number() over (order by (select null)) - 1 as n
          from master.spt_values
         )
    select d.*,
           dateadd(minute, n.n * @intervalMinutes, d.startTime)
    from @myDates d join
         n
         on dateadd(minute, n.n * @intervalMinutes, d.startTime) <= d.endTime;
    
    0 讨论(0)
  • 2020-12-20 10:46

    A numbers/tally table would do the trick as Gordon mentioned. However, I use a UDF to create dynamic date ranges.

    For example

    Select * from [dbo].[udf-Create-Range-Date]('2016-07-10 08:00','2016-07-10 09:00','MI',10)
    

    Returns

    RetVal
    2016-07-10 08:00:00.000
    2016-07-10 08:10:00.000
    2016-07-10 08:20:00.000
    2016-07-10 08:30:00.000
    2016-07-10 08:40:00.000
    2016-07-10 08:50:00.000
    2016-07-10 09:00:00.000
    

    The UDF

    CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int)
    
    Returns 
    @ReturnVal Table (RetVal datetime)
    
    As
    Begin
        With DateTable As (
            Select DateFrom = @DateFrom
            Union All
            Select Case @DatePart
                   When 'YY' then DateAdd(YY, @Incr, df.dateFrom)
                   When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom)
                   When 'MM' then DateAdd(MM, @Incr, df.dateFrom)
                   When 'WK' then DateAdd(WK, @Incr, df.dateFrom)
                   When 'DD' then DateAdd(DD, @Incr, df.dateFrom)
                   When 'HH' then DateAdd(HH, @Incr, df.dateFrom)
                   When 'MI' then DateAdd(MI, @Incr, df.dateFrom)
                   When 'SS' then DateAdd(SS, @Incr, df.dateFrom)
                   End
            From DateTable DF
            Where DF.DateFrom < @DateTo
        )
    
        Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767)
    
        Return
    End
    
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 
    
    0 讨论(0)
  • 2020-12-20 10:51

    You can use recursive query like this :

    declare @intervalMinutes int = 10
    declare @myDates table (
    myId int primary key identity,
    startTime datetime,
    endTime datetime
    )
    
    DECLARE @startTime DATETIME = '2016-07-10 08:00'
    DECLARE @endTime DATETIME = '2016-07-10 09:00'
    
    ;WITH CTE AS
    (
        SELECT  @startTime st
        UNION   ALL
        SELECT  dateadd(MINUTE,@intervalMinutes,st) st
        FROM    cte
        where   dateadd(MINUTE,@intervalMinutes,st) < @endTime
    )
    INSERT INTO @myDates(startTime,endTime)
    SELECT st,dateadd(MINUTE,@intervalMinutes,st) FROM cte
    
    SELECT  * FROm @myDates
    
    0 讨论(0)
提交回复
热议问题