A general date/time function that returns the integer number of intervals of a specified type between two dates.
Synopsis
DATEDIFF(datepart,startdate,enddate)
Arguments
datepart The name (or abbreviation) of a date or time part, specified as a quoted string. This is the unit for which the interval is measured,
startdate The starting date/time for the interval.
enddate The ending date/time for the interval. startdate is subtracted from enddate to determine how many datepart intervals are between the two dates.
Description
The DATEDIFF function returns the INTEGER number of the specified datepart counted within the defined date range. The date range begins at startdate and ends at enddate. (If enddate is earlier than startdate, DATEDIFF returns a negative INTEGER value.)
The datepart parameter can be one of the following date/time components, either the full name (the Date Part column) or its abbreviation (the Abbreviation column). These datepart component names and abbreviations are not case-sensitive.
Date Part Abbreviation
year yy
month mm
week wk
day dd
hour hh
minute mi
second ss
millisecond ms
If you specify an invalid datepart value as a literal, an SQLCODE -8 error code is issued. However, if you supply an invalid datepart value as a host variable, no SQLCODE error is issued and the DATEDIFF function returns a value of NULL.
If you specify a startdate and enddate that include fractional seconds, you can return the difference as a number of fractional seconds, expressed as thousands of a second (.001).
Note that DATEDIFF is provided for Sybase and Microsoft SQL Server compatibility. Similar time/date comparison operations can be performed using the TIMESTAMPDIFF ODBC scalar function.
This function can also be invoked from Caché ObjectScript using the following method call:
$SYSTEM.SQL.DATEDIFF(datepart,startdate,enddate)
Date Expression Formats
The startdate and enddate parameters can be in any of the following formats:
The Caché %String (or compatible) value can be in any of the following formats, and may include or omit fractional seconds:
Sybase/SQL-Server-date is one of these five formats:
mmdelimiterdddelimiter[yy]yy
dd Mmm[mm][,][yy]yy
dd [yy]yy Mmm[mm]
yyyy Mmm[mm] dd
yyyy [dd] Mmm[mm]
where delimiter is a slash (/), hyphen (-), or period (.).
Sybase/SQL-Server-time represents one of these three formats:
HH:MM[:SS:SSS][{AM|PM}]
HH:MM[:SS.S]
HH['']{AM|PM}
If the year is given as two digits, the %DATE utility's sliding-date window is used (if applicable) to determine the century.
Range and Value Checking
DATEDIFF performs the following checks on input values. If a value fails a check, the null string is returned.
Examples
The following example returns 353 because there are 353 days ('dd') between the two timestamps:
SELECT DISTINCT DATEDIFF('dd','1999-01-01 00:00:00','1999-12-20 12:00:00')
FROM sample.person
The following example returns 31 because there are 31 years ('yy') between the two timestamps:
SELECT DISTINCT DATEDIFF('yy','1968-12-21 13:19:00','1999-12-20 00:00:00')
FROM sample.person
Note that the above examples use an abbreviation for the date part. However, you can specify the full name, as in this example:
SELECT DISTINCT DATEDIFF('year','1968-09-10 13:19:00','1999-12-20 00:00:00')
FROM sample.person
See Also
DATEADD DATENAME DATEPART TIMESTAMPADD TIMESTAMPDIFF