database:mssql:sum_datetime_diff

Sum datetime difference in MSSQL

Get the SUM of seconds:

SET @Seconds = SELECT SUM(DATEDIFF(SECOND, [START_TIME], [END_TIME]))

Turn that into a datetime object:

SELECT CONVERT(VARCHAR(8), dateadd(SECOND, @Seconds, 0), 108)

Or as 1 query:

SELECT CONVERT(VARCHAR(8), dateadd(SECOND, SUM(DATEDIFF(SECOND, [START_TIME], [END_TIME])), 0), 108)
Enter your comment:
T L C J Q
 
  • database/mssql/sum_datetime_diff.txt
  • Last modified: 2019-10-31 09:04
  • (external edit)