If you have a column that contains time in seconds and want to convert it to an hour : minute: seconds format, use this query. This query also takes care of displaying hours > 23.
DECLARE @SecondsToConvert int
SET @SecondsToConvert = 10000
-- Declare variables
DECLARE @Hours int
DECLARE @Minutes int
DECLARE @Seconds int
DECLARE @Time datetime
-- Set the calculations for hour, minute and second
SET @Hours = @SecondsToConvert/3600
SET @Minutes = (@SecondsToConvert % 3600) / 60
SET @Seconds = @SecondsToConvert % 60
-- Store the datetime information retrieved in the @Time variable
SET @Time = (SELECT
RTRIM(CONVERT(char(8), @Hours) ) + ':' +
CONVERT(char(2), @Minutes) + ':' +
CONVERT(char(2), @Seconds));
-- Display the @Time variable in the format of HH:MM:SS
SELECT CONVERT(varchar(8),CONVERT(datetime,@Time),108)
About The Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and a new one recently at
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.
Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook
3 comments:
The query does not work for seconds >= 86400 (24 hours or more).
Is there anyway to 36:15:00 (36 hours and 15 minutes)?
Excellent work! Many thanks.
How to convert seconds(Int) to Time more than 86400.
like 45:38:15 (HH:mm:ss)
Post a Comment