mssql 30 minute time intervals beteen 2 datetime

前端 未结 5 1422
余生分开走
余生分开走 2021-01-25 03:10

I have below query and i want to get datetime in 30 min intervals between 2 datetime. Basicly I got it, but is limitited and wouln\'t return al results if the timediff is over 2

5条回答
  •  清歌不尽
    2021-01-25 03:26

    Please see if this works.

    declare @DateTime1 DateTime = '2016-11-24 18:00:00'
    declare @DateTime2 DateTime = '2016-11-25 18:00:00'
    
    declare @Interval DateTime = @DateTime1
    declare @vartmptable table(DT DateTime)
    
    While (@Interval < @DateTime2)
    begin
     --select @Interval, FORMAT(@Interval,'dd-HH:mm')
     insert into @vartmptable select @Interval
     set @Interval = DATEADD(mi,30,@Interval)
    
    end
    
    select FORMAT(DT,'dd-HH:mm')  from @vartmptable
    

提交回复
热议问题