Home Forums SQL Server 2005 T-SQL (SS2K5) Convert DateDiff into Hours, Minutes, Seconds, Milliseconds RE: Convert DateDiff into Hours, Minutes, Seconds, Milliseconds

  • This will return the time difference including milliseconds. It will work fine if the time difference is guaranteed to be less than 24 hours.

    However, if the time difference is greater than 24 hours, it will not give you what you want as it will display the number of hours modulo 24. Also, the time difference calculation will overflow if the difference is greater than about 24.8 days (when number of milliseconds >= 2e31).

    DECLARE @dt1 datetime

    DECLARE @dt2 datetime

    SELECT @dt1 = '20090316 12:00:00.000', @dt2 = GETDATE()

    SELECT CONVERT(varchar(12), DATEADD(ms, DATEDIFF(ms, @dt1, @dt2), 0), 114)