How to compare two dates to find time difference in SQL Server 2005, date manipulation

后端 未结 11 1814
天命终不由人
天命终不由人 2020-11-27 14:55

I have two columns:

job_start                         job_end
2011-11-02 12:20:37.247           2011-11-02 13:35:14.613

How would it be po

11条回答
  •  遥遥无期
    2020-11-27 15:23

    You can use the DATEDIFF function to get the difference in minutes, seconds, days etc.

    SELECT DATEDIFF(MINUTE,job_start,job_end)
    

    MINUTE obviously returns the difference in minutes, you can also use DAY, HOUR, SECOND, YEAR (see the books online link for the full list).

    If you want to get fancy you can show this differently for example 75 minutes could be displayed like this: 01:15:00:0

    Here is the code to do that for both SQL Server 2005 and 2008

    -- SQL Server 2005
    SELECT CONVERT(VARCHAR(10),DATEADD(MINUTE,DATEDIFF(MINUTE,job_start,job_end),'2011-01-01 00:00:00.000'),114)
    
    -- SQL Server 2008
    SELECT CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,job_start,job_end),'2011-01-01 00:00:00.000') AS TIME)
    

提交回复
热议问题