Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come posso mostrare tutto il tempo tra 2 diversi parametri temporali

Usando CTE e supponendo che hour part of @time is zero :

declare @time time(0) = '00:15', 
        @start time(0) = '12:00', 
        @end time(0) = '15:00'

;with cte as (
    select @start sTime, dateadd(minute, datepart(minute,@time), @start) eTime
    union all
    select eTime, dateadd(minute, datepart(minute,@time), eTime)
    from cte
    where dateadd(minute, datepart(minute,@time), eTime) <= @end
)
select left(sTime,5) + ' - ' + left(eTime, 5) results 
from cte

--results
12:00 - 12:15
12:15 - 12:30
12:30 - 12:45
12:45 - 13:00
13:00 - 13:15
13:15 - 13:30
13:30 - 13:45
13:45 - 14:00
14:00 - 14:15
14:15 - 14:30
14:30 - 14:45
14:45 - 15:00