c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
SQL

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

| | 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