Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Team,

I want to display Seconds(Integer) as hh:mm:ss format in SQL Server.
I have seconds column in my table.
e.g. I have Seconds=92730 should be displayed as 25:45:30

How to do this in SQL Query
I have below query to use in Access but how to do this in SQL - Query

(Format(Int(Sum([Seconds])/3600),'00') & ':' & Format(Sum([Seconds]/86400),'nn:ss')) AS [Total Time]
Posted

And a bit different approach.
SQL
DECLARE @RefDate DATETIME = CONVERT(datetime, '01.01.1900',104);
DECLARE @Seconds int = 92730;
DECLARE @CompareDate datetime = DATEADD(second, 92730, @RefDate);

SELECT @Seconds,
       CAST(DATEDIFF(HOUR, @RefDate, @CompareDate) AS varchar(100))
       + ':'
       + CAST(DATEPART(MINUTE, @CompareDate) AS varchar(2))
       + ':'
       + CAST(DATEPART(SECOND, @CompareDate) AS varchar(2))

So if you define a reference date you can use the DATEDIFF to calculate the hours and then extract the minutes and seconds from the date where seconds are added to the reference date.
 
Share this answer
 
Comments
Maciej Los 9-May-12 15:15pm    
Good answer, my 5!
Warning: On MS SQL Server 2005 Express Edition declaring and setting variable in one line is not possible.
Wendelius 9-May-12 16:15pm    
Thanks :) and a good warning since SQL2005 was also tagged.
kevin_ze 10-May-12 3:17am    
Good one,my vote is 5
Wendelius 10-May-12 12:53pm    
Thanks :)
SQL
DECLARE @sec int
DECLARE @tH int
DECLARE @tM int
DECLARE @tS int

SET @sec = 92730
SET @tH = @sec / 3600
SET @tM = @sec / 60 - @tH * 60
SET @tS = @sec - (@tH * 3600 + @tM * 60) 
SELECT CONVERT(NVARCHAR(10),  CONVERT(NVARCHAR(5), @tH) + ':' +  CONVERT(NVARCHAR(5), @tM) + ':' + CONVERT(NVARCHAR(5), @tS)) AS [Total Time]


RESULT for 92730 = 25:45:30

You should write a function[^] or stored procedure[^] to convert seconds into time format.

[EDIT]
As i wrote above you should write custom function...

Copy code below, paste into MS SQL Sercere Manager -> New Query, edit and execute in your database.
This will add UserDefinedFunction [dbo].[GetTimeFromSeconds]
SQL
USE [YourDataBaseName]
GO
/****** Object:  UserDefinedFunction [dbo].[GetTimeFromSeconds]    Script Date: 05/09/2012 18:26:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetTimeFromSeconds] 
(
	-- Add the parameters for the function here
	@sec int
)
RETURNS nvarchar(10)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @tH int
	DECLARE @tM int
	DECLARE @tS int
	DECLARE @Result nvarchar(10)
	SET @tH = @sec / 3600
	SET @tM = @sec / 60 - @tH * 60
	SET @tS = @sec - (@tH * 3600 + @tM * 60) 

	-- Add the T-SQL statements to compute the return value here
	SELECT @Result = CONVERT(NVARCHAR(10),  CONVERT(NVARCHAR(5), @tH) + ':' +  CONVERT(NVARCHAR(5), @tM) + ':' + CONVERT(NVARCHAR(5), @tS)) 

	-- Return the result of the function
	RETURN @Result

END


Usage:
SQL
SELECT  T.[Seconds], YourDataBaseName.[dbo].[GetTimeFromSeconds] (T.[Seconds]) AS [Total Time]
FROM YourTableName AS T


[/EDIT]
 
Share this answer
 
v2
Comments
Pravinkarne.31 9-May-12 12:12pm    
Thanks!!!, This is correct, But I have data in column and I want to use same query in my C# code to populate DataGridView showing total time in [h]:mm:ss
What will be the best way to do this?
Maciej Los 9-May-12 12:38pm    
As i wrote in my answer, you should write custom function...
Check my answer after update.
Wendelius 9-May-12 16:16pm    
Looks working to me :) 5+
Maciej Los 9-May-12 16:39pm    
Thank you ;)
try something like this:

SQL
DECLARE @seconds int;
SET @seconds = 14400;

SELECT DATEADD(second, @seconds, '20120509')
,CONVERT(char(8), DATEADD(second, @seconds, '20120509'), 108);
 
Share this answer
 
v2
Comments
Maciej Los 9-May-12 10:16am    
Wrong answer! Check result.
Herman<T>.Instance 9-May-12 13:49pm    
easy downvote. If you change char(5) to char(8)!!!!!

result:
(No column name) (No column name)
2012-05-09 04:00:00.000 04:00:00
Maciej Los 9-May-12 14:11pm    
"easy downvote" Yes, if solution is wrong, but it's easy to re-vote.
If @seconds = 92730 then hours > 24. That's why the result is: 2012-05-10 01:45:30.000 and 01:45:30
Maciej Los 9-May-12 14:24pm    
And another one thing... It is easy to downvote anonymus. If my answer is wrong, you should write why!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900