converting minutes to hour:minutes:second in sql

后端 未结 2 1240
孤城傲影
孤城傲影 2020-12-22 14:37

I have a table transaction like this:

transactID    Paydate                   DelDate                     vtid
24            2013-05-08 16:53:03         


        
相关标签:
2条回答
  • 2020-12-22 15:03

    If you want to convert minutes or seconds to hh:mm:ss format then you could use these functions (DATEADD,CONVERT with style 114 and LEFT):

    DECLARE @PayDate     DATETIME,
            @DelDate     DATETIME,
            @DiffSeconds INT,
            @DiffMinutes INT;
    
    SELECT  @PayDate='2013-05-08T16:53:03.000',
            @DelDate='2013-05-08T17:00:28.000',
            @DiffSeconds=DATEDIFF(SECOND,@PayDate,@DelDate),
            @DiffMinutes=DATEDIFF(MINUTE,@PayDate,@DelDate);
    
    
    SELECT  @DiffSeconds AS Diff_Seconds,
            LEFT(CONVERT(VARCHAR(50),DATEADD(SECOND,@DiffSeconds,0),114),8) AS Diff_From_Seconds,
            @DiffMinutes AS Diff_Minutes,
            LEFT(CONVERT(VARCHAR(50),DATEADD(MINUTE,@DiffMinutes,0),114),8) AS Diff_From_Minutes;
    

    Results:

    Diff_Seconds Diff_From_Seconds Diff_Minutes Diff_From_Minutes
    ------------ ----------------- ------------ -----------------
    445          00:07:25          7            00:07:00
    
    0 讨论(0)
  • 2020-12-22 15:16

    marc_s is right with his comment. What you need is a function like in this answer.

    You would then call it like this:

    SELECT 
         udfTimeSpanFromSeconds(SUM(DATEDIFF(SECOND, t.Paydate, t.DelDate))) AS 'HH:MM:SS'
    FROM 
         Transaction_tbl t 
    WHERE 
         t.transactID in (24, 25) 
    group by 
         vtid 
    
    0 讨论(0)
提交回复
热议问题