且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 T-SQL 中生成两个日期之间的分钟间隔?

更新时间:2022-04-14 09:11:05

数字表可以解决您的问题.假设您不需要超过几千行,那么这应该可行:

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;