Eureka Moment Wiki

One eureka moment at the time

User Tools

Site Tools


database:mssql:sum_datetime_diff

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

database:mssql:sum_datetime_diff [2017/08/10 12:59]
database:mssql:sum_datetime_diff [2019/10/31 09:04] (current)
Line 1: Line 1:
 +====== Sum datetime difference in MSSQL ======
 +Get the SUM of seconds:
 +
 +<​code>​SET @Seconds = SELECT SUM(DATEDIFF(SECOND,​ [START_TIME],​ [END_TIME]))</​code>​
 +
 +Turn that into a datetime object:
 +
 +<​code>​SELECT CONVERT(VARCHAR(8),​ dateadd(SECOND,​ @Seconds, 0), 108)</​code>​
 +
 +Or as 1 query:
 +
 +<​code>​SELECT CONVERT(VARCHAR(8),​ dateadd(SECOND,​ SUM(DATEDIFF(SECOND,​ [START_TIME],​ [END_TIME])),​ 0), 108)</​code>​
 +
 +