我想编写sql查询,它将给出如下的put 查询:
DivideTime('10','10:00:00','11:00:00')
输出:
10:10
10:20
10:30
10:40
10:50
11:00
答案 0 :(得分:1)
这可以使用递归公用表表达式
来完成CREATE FUNCTION [dbo].[get_time_range] (@interval INT, @start_time TIME, @end_time TIME)
RETURNS @tbl TABLE ([time] TIME)
AS
BEGIN
WITH [cte_recursion] AS
(
SELECT DATEADD(MINUTE, @interval, @start_time) [time]
UNION ALL
SELECT
DATEADD(MINUTE, @interval, [time]) [time]
FROM [cte_recursion]
WHERE [time] < @end_time
)
INSERT INTO @tbl ([time])
SELECT
[time]
FROM [cte_recursion]
OPTION (MAXRECURSION 0); -- allows more than 100 iterations
RETURN;
END
GO
SELECT
*
FROM [dbo].[get_time_range](10, '10:00:00', '11:00:00');
答案 1 :(得分:1)
使用numbers
表:
select dateadd(minute, 10*n, @start_time)
from numbers n
where n <= datediff(minute, @start_time, @end_time)/@interval