DATEDIFF()
Difference between two datetime values in a datetime unit
BIGINT
, or NUMERIC(18,1)
for MILLISECOND
DATEDIFF (<args>) <args> ::= <unit> FROM <moment1> TO <moment2> | <unit>, <moment1>, <moment2> <unit> ::= YEAR | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND | MILLISECOND <momentN> ::= a DATE, TIME or TIMESTAMP expression
Parameter | Description |
---|---|
unit |
Date/time unit |
moment1 |
An expression of the |
moment2 |
An expression of the |
Returns the number of years, months, weeks, days, hours, minutes, seconds or milliseconds elapsed between two date/time values.
-
DATE
andTIMESTAMP
arguments can be combined.No other mixes are allowed. -
With
TIMESTAMP
andDATE
arguments, all units can be used. -
With
TIME
arguments, onlyHOUR
,MINUTE
,SECOND
andMILLISECOND
can be used.
-
DATEDIFF
doesn’t look at any smaller units than the one specified in the first argument.As a result,-
datediff (year, date '1-Jan-2009', date '31-Dec-2009')
returns 0, but -
datediff (year, date '31-Dec-2009', date '1-Jan-2010')
returns 1
-
-
It does, however, look at all the bigger units. So:
-
datediff (day, date '26-Jun-1908', date '11-Sep-1973')
returns 23818
-
-
A negative result value indicates that moment2 lies before moment1.