Difference of two date time in sql server

后端 未结 20 2043
心在旅途
心在旅途 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:55

    Internally in SQL Server dates are stored as 2 integers. The first integer is the number of dates before or after the base date (1900/01/01). The second integer stores the number of clock ticks after midnight, each tick is 1/300 of a second.

    More info here

    Because of this, I often find the simplest way to compare dates is to simply substract them. This handles 90% of my use cases. E.g.,

    select date1, date2, date2 - date1 as DifferenceInDays
    from MyTable
    ...
    

    When I need an answer in units other than days, I will use DateDiff.

提交回复
热议问题