Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

converting milliseconds Expand / Collapse
Author
Message
Posted Wednesday, February 25, 2009 1:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 20, 2009 3:23 AM
Points: 39, Visits: 200
Hi,

how to convert milliseconds into hours,minutes,seconds and milliseconds.

i have two dates from that i have taken milli seconds using datediff.now i want to display total hours,minutes,sec,millisec in that milli seconds.


Thanks,
Ramu
Post #664059
Posted Wednesday, February 25, 2009 2:07 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, October 07, 2011 1:41 AM
Points: 346, Visits: 534
ramu4ind (2/25/2009)
Hi,

how to convert milliseconds into hours,minutes,seconds and milliseconds.

i have two dates from that i have taken milli seconds using datediff.now i want to display total hours,minutes,sec,millisec in that milli seconds.


For calculating the time difference, directly subtract two dates from each other as



Thanks,
Ramu


kshitij kumar
kshitij@krayknot.com
www.krayknot.com
Post #664075
Posted Wednesday, February 25, 2009 2:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 1:27 AM
Points: 2,366, Visits: 1,837
krayknot

your answer is not complete.


"Keep Trying"
Post #664076
Posted Wednesday, February 25, 2009 2:14 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 8:46 AM
Points: 335, Visits: 1,956
declare @milli as int
set @milli = 11202079

select @milli/3600000 as hours,
(@milli - ((@milli/3600000)*3600000))/60000 as minutes,
(@milli - (((@milli)/60000)*60000)) /1000 as seconds,
@milli-(((@milli)/1000)*1000) as milliseconds
Post #664077
Posted Wednesday, February 25, 2009 2:29 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, April 11, 2014 6:10 AM
Points: 1,135, Visits: 317
DECLARE @ms AS INTEGER
SELECT @ms = 86399998

SELECT CONVERT (CHAR(13), DATEADD (ms, @ms, '01/01/00'), 14)

Obviously this would be the maximum number that it would show after this you would need to handle days - Remember that milliseconds are displayed and rounded to 1/300 of a second
Post #664083
Posted Wednesday, February 25, 2009 2:33 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 26, 2011 7:31 AM
Points: 75, Visits: 262
Going back to school

declare @MILI bigint
set @mili= 1*60*60*1000+2010

select @mili
,@Mili/(1000*60*60) as Hour
,(@Mili%(1000*60*60))/(1000*60)as Minutes
,((@Mili%(1000*60*60))%(1000*60))/1000 as Seconds
,((@Mili%(1000*60*60))%(1000*60))%1000 as MiliSecs



I Like CTE
My Blog - Arz Kiya Hai!!
Post #664085
Posted Wednesday, March 04, 2009 7:30 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:39 PM
Points: 35,977, Visits: 30,267
ramu4ind (2/25/2009)
Hi,

how to convert milliseconds into hours,minutes,seconds and milliseconds.

i have two dates from that i have taken milli seconds using datediff.now i want to display total hours,minutes,sec,millisec in that milli seconds.


Thanks,
Ramu


Heh... lemme ask why you want to do this? Is it for display purposes? If so, then the following will probably do with the understanding that this one only goes up to 24 hours... and there's no need to convert it to milliseconds...

SELECT CONVERT(CHAR(12),@EndDate-@StartDate,114)

If you need more than 24 hours, then the following will do...
SELECT CONVERT(VARCHAR(10),DATEDIFF(hh,0,@EndDate-@StartDate)) --Converts the hours
+RIGHT(CONVERT(CHAR(8),@EndDate-@StartDate,108),6) --Converts mins & secs
AS [HHHHH:MM:SS]

... but, I have to tell you, unless it's for output to a file, you shouldn't do this type of formatting in T-SQL... especially breaking it into separate columns. Certainly, you shouldn't store the data that way. If you need to store the difference in a table, store it as a DateTime datatype... that way, you can add them up later on and display the total either in a GUI or using one of the other conversion methods on this thread for the display.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #668210
Posted Friday, May 13, 2011 8:16 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 8:30 AM
Points: 73, Visits: 152
CONVERT(TIME,DATEADD (ms, @YOURMILLISECONDS, 0))
Post #1108456
Posted Friday, May 13, 2011 8:22 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, January 16, 2014 12:03 PM
Points: 21,376, Visits: 9,594
chris.rogers00 (5/13/2011)
CONVERT(TIME,DATEADD (ms, @YOURMILLISECONDS, 0))


This is sql 2k5, time is not available.
Post #1108460
Posted Friday, May 13, 2011 8:35 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 8:30 AM
Points: 73, Visits: 152
gutted, sorry.. Should have read the post..

;)
Post #1108474
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse