SQL

How to generate a row of every 15 minutes of the start time and end time?

How to generate a row of every 15 minutes of the start time and end time?, someone asked me to explain?
SQL

I have the specified start date time and end date time i.e (end date time is end of sequence), I add a time interval (this can vary) to the start date time in minutes and this gives me the end date time. we can set time interval 15minutes (60 sec*15 min=900sec).

Execute the SQL Command:


declare @StartTime datetime = '2016-02-18 09:00:00',
@EndTime datetime = '2016-02-18 18:00:00',
@Interval int = 900 -- this can be changed.;WITH timeSequence AS
(
SELECT
@StartTime AS StartRange,
DATEADD(SECOND, @Interval, @StartTime) AS EndRange
UNION ALL
SELECT
EndRange,
DATEADD(SECOND, @Interval, EndRange)
FROM timeSequence
WHERE DATEADD(SECOND, @Interval, EndRange) < @EndTime
)

SELECT * FROM timeSequence OPTION (MAXRECURSION 0);

StartRange EndRange
2/18/2016 9:00:00 AM 2/18/2016 9:15:00 AM
2/18/2016 9:15:00 AM 2/18/2016 9:30:00 AM
2/18/2016 9:30:00 AM 2/18/2016 9:45:00 AM

Post your comments / questions