sqlserver calculates the time difference DATEDIFF function

DATEDIFF function [date and time]

The function
returns the interval between two dates.

语法
DATEDIFF ( date-part, date-expression-1, date-expression-2 )

date-part :
year | quarter | month | week | day | hour | minute | second | millisecond

The parameter
date-part specifies the part of the date whose interval is to be measured.

For more information on the date section, see Date Section.

date-expression-1 The starting date of an interval. Subtracts this value from date-expression-2 to return the number of date-parts in days between the two arguments.

date-expression-2 The end date of an interval. Subtract Date-expression-1 from this value to return the number of days in date-parts between the two arguments.

Usage
This function counts the number of date parts between two specified dates. The result is a signed integer value equal to (date2 - date1) in the date part.

DATEDIFF is truncated rather than rounded when the result is not an even multiple of the date part.

When using day as the date part, DATEDIFF returns the number of midnights between the two specified times (including the second date but not the first).

When month is used as the date part, DATEDIFF returns the number of first days of the month that occur between two dates, including the second date but not the first.

When week is used as the date part, DATEDIFF returns the number of Sundays between two dates (including the second date but not the first).

There are overflow values ​​for smaller time units:

milliseconds 24 days

seconds 68 years

minutes 4083 years

others have no overflow limit

If these limits are exceeded, this function will return an overflow error.

Standards and Compatibility
SQL /92 Transact- SQL  extensions.

SQL /99 Transact- SQL  extension.

Sybase is compatible with Adaptive Server Enterprise.

The following example statement returns 1:

The statement below SELECT datediff( hour, '4:00AM', '5:50AM' ) returns 102:

SELECT datediff( month, '1987/05/02', '1995/11/15' ) The following statement returns 0:

The statement below SELECT datediff( day, '00:00', '23:59' ) returns 4:

SELECT datediff( day,'1999/07/19 00:00','1999/07/23 23:59' ) The following statement returns 0:

SELECT datediff( month, '1999/07/19', '1999/07/23' ) The following statement returns 1:

SELECT datediff( month, '1999/07/19', '1999/08/23' )

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327101728&siteId=291194637