Returns the interval between two dates.
Parameter |
Description |
---|---|
date-part |
Specifies the date part in which the interval is to be measured. |
date-expression1 |
The starting date for the interval. This value is subtracted from date-expression2 to return the number of date parts between the two arguments. |
date-expression2 |
The ending date for the interval. date-expression1 is subtracted from this value to return the number of date parts between the two arguments. |
INT
The following statement returns 1:
SELECT DATEDIFF( HOUR, '4:00AM', '5:50AM' ) FROM iq_dummy
The following statement returns 102:
SELECT DATEDIFF( MONTH, '1987/05/02', '1995/11/15' ) FROM iq_dummy
The following statement returns 0:
SELECT DATEDIFF( DAY, '00:00', '23:59' ) FROM iq_dummy
The following statement returns 4:
SELECT DATEDIFF( DAY, '1999/07/19 00:00', '1999/07/23 23:59' ) FROM iq_dummy
The following statement returns 0:
SELECT DATEDIFF( MONTH, '1999/07/19', '1999/07/23' ) FROM iq_dummy
The following statement returns 1:
SELECT DATEDIFF( MONTH, '1999/07/19', '1999/08/23' ) FROM iq_dummy
SELECT DATEDIFF(MCS, '2009-11-03 11:10:42.033185', '2009-11-03 11:10:42.033189') FROM iq_dummy
SELECT DATEDIFF(MICROSECOND, '2009-11-10 14:57:52.722001', '2009-11-10 14:57:52.722016') FROM iq_dummy
SELECT DATEDIFF(MCS, '2000/07/07/07 07:07:06.277777', '2000/07/07/07 07:07:07.777777') FROM iq_dummy
This function calculates the number of date parts between two specified dates. The result is a signed integer value equal to (date2 - date1), in date parts.
DATEDIFF results are truncated, not rounded, when the result is not an even multiple of the date part.
SELECT DATEDIFF( DAY, '2003/08/03 14:00', '2003/08/08 14:00' ) FROM iq_dummy
SELECT DATEDIFF( MONTH, '2003/02/01', '2003/11/15' ) FROM iq_dummy; SELECT DATEDIFF( MONTH, '2003/02/01', '2003/11/01' ) FROM iq_dummy;
The first date 2003/02/01 is a first-of-month, but is not included in the result of either query. The second date 2003/11/01 in the second query is also a first-of-month and is included in the result.
SELECT DATEDIFF( week, '2003/08/03', '2003/08/31' ) FROM iq_dummy;The first Sunday (2003/08/03) is not included in the result.