2014-06-27 102 views
0

有人可以指導我如何將秒轉換爲總小時,分鐘和秒鐘格式?將秒數轉換爲總時數:分鐘:秒

例如:2652819秒應該給736:53:39

我能夠得到同樣的days:hours:minutes:seconds格式,但我特別要求轉換也是天進總小時

+0

看看你有幾天的代碼,並帶走了天的一部分。 –

回答

2

假設你有一個積分值tot_secs —浮點需要一些明智地使用round()ceil()和/或floor(),但你可以嘗試這樣的事情:

select hours = (t.tot_secs/3600)  , -- hours is total seconds/(secs/hr) 
     minutes = (t.tot_secs % 3600)/60 , -- minutes is whatever's left over/60 
     seconds = t.tot_secs % 60 , -- whatever's left over is seconds 
     hh_mm_ss =     convert(varchar, t.tot_secs/3600) 
       + ':' + right('0' + convert(varchar, (t.tot_secs % 3600)/60) , 2) 
       + ':' + right('0' + convert(varchar, t.tot_secs   % 60) , 2) 
from (select tot_secs = 2652819 
    ) t 

以上查詢產生

hours minutes seconds hh_mm_ss 
----- ------- ------- -------- 
736  53  39 736:53:39 
0

這工作:

DECLARE @Seconds INT 
SET @Seconds = 2652819 

SELECT CAST(@Seconds/3600 AS VARCHAR(10)) + ':' + 
     CAST((@Seconds%3600)/60 AS VARCHAR(10)) + ':' + 
     CAST(@Seconds%60 AS VARCHAR(2)) 
0

這將解析秒爲幾天:小時:分鐘:秒。

declare @seconds int = 2652819 
select @seconds/(60 * 60 * 24) [days], (@seconds/(60 * 60)) % 60 [hours], (@seconds/60) % 60 [minutes], @seconds % 60 seconds 
select cast(@seconds/(60 * 60 * 24) as varchar(10)) + ':' + 
     cast((@seconds/(60 * 60)) % 60 as varchar(2)) + ':' + 
     cast((@seconds/60) % 60 as varchar(2)) + ':' + 
     cast(@seconds % 60 as varchar(2)) as [days:hours:minutes:seconds] 

輸出:

days|hours|minutes|seconds 
----|-----|-------|------- 
30 | 16 | 53 | 39 

days:hours:minutes:seconds 
-------------------------- 
30:16:53:39