SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


converting milliseconds


converting milliseconds

Author
Message
ramu4ind
ramu4ind
SSC Veteran
SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)

Group: General Forum Members
Points: 265 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
krayknot
krayknot
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3472 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
ChiragNS
ChiragNS
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11269 Visits: 1865
krayknot

your answer is not complete.

"Keep Trying"
Samuel Vella
Samuel Vella
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3511 Visits: 2144
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
Andy Reilly
Andy Reilly
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2112 Visits: 407
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
JohnSmith001
JohnSmith001
Old Hand
Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)Old Hand (333 reputation)

Group: General Forum Members
Points: 333 Visits: 281
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



John Smith
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)SSC Guru (378K reputation)

Group: General Forum Members
Points: 378884 Visits: 42941
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
chris.rogers00
chris.rogers00
SSC-Addicted
SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)

Group: General Forum Members
Points: 416 Visits: 187
CONVERT(TIME,DATEADD (ms, @YOURMILLISECONDS, 0))
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116448 Visits: 9672
chris.rogers00 (5/13/2011)
CONVERT(TIME,DATEADD (ms, @YOURMILLISECONDS, 0))


This is sql 2k5, time is not available.
chris.rogers00
chris.rogers00
SSC-Addicted
SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)SSC-Addicted (416 reputation)

Group: General Forum Members
Points: 416 Visits: 187
gutted, sorry.. Should have read the post..

Wink
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search