I’m need a help to create a Query. My problem is I have a StartDate and EndDate and need separate this in blocs of 60 minutes.
DECLARE @STARTDATE AS SMALLDATETIME
DECLARE @ENDDATE AS SMALLDATETIME
SET @STARTDATE = '2012-11-21 11:03:00'
SET @ENDDATE = '2012-11-21 13:04:00'
I need the return:
Hour, Time
11 , 57
12 , 60
13 , 04
You could use a recursive CTE. For example:
The snippet
dateadd(hour, datediff(hour, 0, dt), 0)removes the hours and minutes from a date. It does so by calculating the number of hours since date0and then adding that number of hours to date0.Live example at SQL Fiddle.