There's no built-in function - but you could relatively easily write your own T-SQL stored function to calculate this - something like this:
CREATE FUNCTION dbo.TimeDifference (@FromTime TIME(7), @ToTime TIME(7))
RETURNS VARCHAR(10)
AS BEGIN
DECLARE @Diff INT = DATEDIFF(SECOND, @FromTime, @ToTime)
DECLARE @DiffHours INT = @Diff / 3600;
DECLARE @DiffMinutes INT = (@Diff % 3600) / 60;
DECLARE @DiffSeconds INT = ((@Diff % 3600) % 60);
DECLARE @ResultString VARCHAR(10)
SET @ResultString = RIGHT('00' + CAST(@DiffHours AS VARCHAR(2)), 2) + ':' +
RIGHT('00' + CAST(@DiffMinutes AS VARCHAR(2)), 2) + ':' +
RIGHT('00' + CAST(@DiffSeconds AS VARCHAR(2)), 2)
RETURN @ResultString
END
This function uses the integer division (/
) and integer remainder (%
) operators to calculate the number of hours, minutes and seconds that those two times are apart, and then concatenates those together into a string as you are looking for.
SELECT
dbo.TimeDifference('13:50:00', '14:10:00'),
dbo.TimeDifference('13:50:00', '15:51:05'),
dbo.TimeDifference('13:50:00', '15:35:45')
Sample output:
00:20:00 02:01:05 01:45:45