Monday, March 2, 2009

Calculate DateDiff and Convert Seconds to Hours/Minutes


You can use this simple DATEDIFF function to calculate the difference, in seconds, between a SubmitDate and CompleteDate.

SELECT DATEDIFF(s, SubmitDate,CompleteDate)
FROM Table
WHERE Column1=ID Value


This will allow you to use the result from above (seconds) to convert the seconds into HH:MM format.

DECLARE @NumberOfSeconds INT
SET @NumberOfSeconds = Result from above
SELECT CAST(@NumberOfSeconds / 3600 AS VARCHAR(3)) + ':' +
RIGHT('0' + CAST(@NumberOfSeconds % 3600 / 60 AS VARCHAR(2)), 2)

No comments: