Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Convert Int value of seconds into a Time Stamp

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
EHardie
Asked:
EHardie
  • 3
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
EHardieAuthor Commented:
Thanks this is perfect !
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now