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

后端 未结 11 1810
天命终不由人
天命终不由人 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:26

    Declare the Start and End date DECLARE @SDATE AS DATETIME

    TART_DATE  AS DATETIME
    DECLARE @END_-- Set Start and End date
    SET @START_DATE = GETDATE()
    SET @END_DATE    = DATEADD(SECOND, 3910, GETDATE())
    

    -- Get the Result in HH:MI:SS:MMM(24H) format SELECT CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS, @START_DATE, @END_DATE), 0), 114) AS TimeDiff

    0 讨论(0)
  • 2020-11-27 15:31

    Take a look at the DateDiff() function.

    -- Syntax
    -- DATEDIFF ( datepart , startdate , enddate )
    
    -- Example usage
    SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff
    SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff
    SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff
    SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff
    SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1) AS HourDiff
    ...
    

    You can see it in action / play with it here

    0 讨论(0)
  • 2020-11-27 15:33

    If your database StartTime = 07:00:00 and endtime = 14:00:00, and both are time type. Your query to get the time difference would be:

    SELECT TIMEDIFF(Time(endtime ), Time(StartTime )) from tbl_name
    

    If your database startDate = 2014-07-20 07:00:00 and endtime = 2014-07-20 23:00:00, you can also use this query.

    0 讨论(0)
  • 2020-11-27 15:35

    Take a look at DATEDIFF, this should be what you're looking for. It takes the two dates you're comparing, and the date unit you want the difference in (days, months, seconds...)

    0 讨论(0)
  • 2020-11-27 15:38

    I used following logic and it worked for me like marvel:

    CONVERT(TIME, DATEADD(MINUTE, DATEDIFF(MINUTE, AP.Time_IN, AP.Time_OUT), 0)) 
    
    0 讨论(0)
提交回复
热议问题