I'm currently looking at finding time differences between dates and outputting to hours (00:00). However, I'm then going on to sum this calculation which means I can't convert to char/varchar.
Please see example data below how I would like it to appear.
EMPLOYEE_ID Start End Duration
1 05/06/2015 08:30 05/06/2015 12:12 03:42
1 05/06/2015 13:18 05/06/2015 17:00 03:42
1 06/06/2015 08:30 06/06/2015 12:12 03:42
1 06/06/2015 13:18 06/06/2015 17:00 03:42
Current code for Duration:
CONVERT(varchar(5),DATEADD(minute,DATEDIFF(minute,Start, End),0), 114) as Duration
Obviously, this code is not how I want it as it's varchar and I'd like to go on to sum two durations together. I have looked around but can't find anything which shows how this would be done.
SQL Server doesn't currently supoort the ANSI SQL
INTERVAL
data type so about the best you can do to support aggregation is calculate the duration in a consistent unit of measure (like minutes you used here) and then use the aggregation result to calculate the interval from a fixed datetime value. Although the example below uses theFORMAT
function to convert the result to a string, I suggest formatting for display purposes be done in the presentation layer rather than T-SQL.