如何使用递归 CTE 获取从 00 到 23 的时间?

How to get time from 00 to 23 using recursive CTE?

如何使用递归 CTE 获取一天中从 00 点到 23 点的小时数?

它给出了 00 到 24 小时,但我需要在我的结果集中排除 24 小时,或者换句话说,我只需要最多 00 到 23 小时

我的代码:

DECLARE @calenderDate DATETIME2(0) = '2019-05-16 05:00:00'
DECLARE @hr1Week int = 0

;with numcte AS  
       (  
         SELECT 0 [num]  
         UNION all  
         SELECT [num] + 1 FROM numcte WHERE [num] < (Select  datediff(HOUR, @hr1Week, dateadd(DAY, 1, @hr1Week)))
       )        

select * from numcte

它给出了 00 到 24 小时,但我需要在我的结果集中排除 24 小时,或者换句话说,我只需要最多 00 到 23 小时

实际结果:

num
----
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

预期结果:

num
---
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

而不是
Select datediff(HOUR, @hr1Week, dateadd(DAY, 1, @hr1Week)),改为
Select datediff(HOUR, @hr1Week, dateadd(DAY, 1, @hr1Week)) - 1
return 最多只能到 23 小时

DECLARE @calenderDate DATETIME2(0) = '2019-05-16 05:00:00'
DECLARE @hr1Week int = 0

;with numcte AS  
(  
     SELECT 0 [num]  
     UNION all  
     SELECT [num] + 1 FROM numcte WHERE [num] < 
      (SEELCT datediff(HOUR, @hr1Week, dateadd(DAY, 1, @hr1Week)) -1)
)        

SELECT * FROM numcte

Demo on db<>fiddle