Solved

Convert Int value of seconds into a Time Stamp

Posted on 2008-10-24
4
328 Views
Last Modified: 2012-05-05
Hi ,

I have an Int field containing seconds that I need to convert into a Time Stamp.
I have managed to select
Select CONVERT(char(8), DATEADD(second, ActualDuration, ''), 108)
From
Table

But it does not give me a 24 hour clock in the sense that if I have a value of 173690
it is displaying as 00:14:50  
This is incorrect.
I need it to add on 24 hours per day, so if the int values of seconds goes into days I need the time stamp to display 48:00:00 for example when it's been 2 days.

Help would be appreciated....



0
Comment
Question by:EHardie
  • 3
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22798713
Take a look at this user defined function you can create to emulate TimeSerial on MS SQL Server.  To make it function they way you want, just remove the % 24 part of this line:
SET @hours = floor(@total/3600) % 24

Here is the referenced link:
http://crossedlogic.blogspot.com/2008/09/group-by-time-timeserial-makes-return.html
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22798766
And you will also need to remove the code to adjust from millitary time.  Here is a way to do it directly in SQL without UDF.

SELECT Cast((ActualDuration/3600) As nvarchar(2)) + ':' + RIGHT('0'+Cast(((ActualDuration % 3600)/60) As nvarchar(2)), 2)    SET @result = @result + ':' + RIGHT('0'+Cast(((ActualDuration % 3600) % 60) As nvarchar(2)),2)

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22798777
Arg!  Sorry, got some residual text in the code snippet -- use this one.  Good thing I wrote the original, so I can copy all I want. ;)
SELECT Cast((ActualDuration/3600) As nvarchar(2)) + ':' + RIGHT('0'+Cast(((ActualDuration % 3600)/60) As nvarchar(2)), 2) + ':' + RIGHT('0'+Cast(((ActualDuration % 3600) % 60) As nvarchar(2)),2)

Open in new window

0
 

Author Closing Comment

by:EHardie
ID: 31509756
Thanks this is perfect !
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 32
Help Required 2 29
MS SQL with ODBC 5 34
SSRS 2013 - Creating a summarized report 19 32
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now