将秒转换为小时:分钟:秒

时间:2016-01-11 10:29:19

标签: sql sql-server

我似乎已经为此提出了一些问题但是我无法对我所拥有的SQL进行调整,因为我发现在加入其他表时它很复杂。

我有一个查询告诉我特定作业在运行时已经运行了多长时间,但它会在几秒钟内告诉我。我想将这些秒转换为格式hours:minuteshours:minutes:seconds

谢谢!

USE msdb
SELECT job.name AS [Job Name],
       activity.run_requested_date AS [Run Date And Time],
       DATEDIFF( SECOND, activity.run_requested_date, GETDATE() ) AS [Elapsed]
FROM sysjobs_view job WITH (NOLOCK)
LEFT JOIN sysjobactivity activity ON job.job_id = activity.job_id
LEFT JOIN syssessions sess ON sess.session_id = activity.session_id
LEFT JOIN (SELECT MAX( agent_start_date ) AS max_agent_start_date
           FROM syssessions WITH (NOLOCK)) sess_max
    ON sess.agent_start_date = sess_max.max_agent_start_date
WHERE run_requested_date IS NOT NULL
  AND stop_execution_date IS NULL

1 个答案:

答案 0 :(得分:2)

采用以下示例:

DECLARE @dt1 DATETIME='2016-01-11T12:00:00';
DECLARE @dt2 DATETIME='2016-01-07T20:30:15';

SELECT
    CAST(DATEDIFF(s,@dt2,@dt1)/3600 AS VARCHAR(4))+':'+
    CAST(DATEDIFF(s,@dt2,@dt1)%3600/60 AS VARCHAR(2))+':'+
    CAST(DATEDIFF(s,@dt2,@dt1)%3600%60 AS VARCHAR(2));

这将打印87:29:45,即87小时29分45秒。使用此示例并将其应用于您的查询。 GL!