IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'CF_TIMEDIFF') 
                   and type = 'FN')
BEGIN
    DROP FUNCTION CF_TIMEDIFF
END
GO


CREATE FUNCTION CF_TIMEDIFF (@sd DATETIME, @ed DATETIME, @type CHAR(1)) 
RETURNS VARCHAR(30)
AS
BEGIN
	DECLARE @i INT
		,@Ret VARCHAR(30)

	SET @i = DATEDIFF(SECOND, @sd, @ed) 
	--SET @i = DATEDIFF(SECOND, @sd, CONVERT(CHAR(10), @sd, 126) + 
	--		'T17:00:00.000') + ((DATEDIFF(DAY, @sd, @ed) - 1) * (8 * 60 * 60)) +
	--		DATEDIFF(SECOND, CONVERT(CHAR(10), @ed, 126) + 'T08:00:00.000', @ed)
	IF UPPER(@type) = 'H' 
		SET @Ret =	CONVERT(VARCHAR(5), (@i / 3600) )			+ ':' +
					RIGHT(CONVERT(VARCHAR(3), ((@i % 3600) / 60) + 100 ),2)	+ ':' +
					RIGHT(CONVERT(VARCHAR(3), ((@i % 3600) % 60) + 100 ),2)
  

	RETURN @Ret
END

/*

SELECT dbo.CF_TIMEDIFF('20220101 10:30','20220102 11:35','H')

*/