Skip to content
Advertisement

Is it possible to sum up time field in SQL Server?

Example

I have 3 time(0) column in 1 row. I want to calculate arrival-Time column automatically and this is result of the sum of Departure-Time + Duration

Departure-Time= 14:30:00
Duration:     =  02:45:00
Arrival must be = 17:15:00

is it possible with trigger or computed area method? thanks in advance

Advertisement

Answer

SQL Server does not support direct addition on time values; however, you can use datediff against 0 to find how many (of some interval – minutes, seconds, etc; choice depends on your required precision) to calculate the duration, and then dateadd the same:

declare @start time= '14:30:00', @duration time = '02:45:00';

declare @end time = dateadd(second, datediff(second, 0, @duration), @start);

select @end; -- 17:15:00.0000000
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement