Difference of two date time in sql server

后端 未结 20 2103
心在旅途
心在旅途 2020-12-01 03:53

Is there any way to take the difference between two datetime in sql server?

For example, my dates are

  1. 2010-01-22 15:29:55.090
20条回答
  •  醉酒成梦
    2020-12-01 04:38

    Just a caveat to add about DateDiff, it counts the number of times you pass the boundary you specify as your units, so is subject to problems if you are looking for a precise timespan. e.g.

    select datediff (m, '20100131', '20100201')
    

    gives an answer of 1, because it crossed the boundary from January to February, so even though the span is 2 days, datediff would return a value of 1 - it crossed 1 date boundary.

    select datediff(mi, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153')
    

    Gives a value of 1, again, it passed the minute boundary once, so even though it is approx 14 seconds, it would be returned as a single minute when using Minutes as the units.

提交回复
热议问题