Datediff Function; Examples Of Datediff - HP Neoview SQL Reference Manual

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

DATEDIFF Function

The DATEDIFF function returns the integer value for the number of datepart units of time
between startdate and enddate. If enddate precedes startdate, the return value is
negative or zero.
DATEDIFF is a Neoview extension.
DATEDIFF (datepart, startdate, enddate)
datepart
is YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, QUARTER, WEEK, or one of the
following abbreviations
YY and YYYY
M and MM
D and DD
HH
MI and M
SS and S
Q and QQ
WW and WK
startdate
may be of type DATE or TIMESTAMP. See
enddate
may be of type DATE or TIMESTAMP. See
The method of counting crossed boundaries such as days, minutes, and seconds makes the result
given by DATEDIFF consistent across all data types. The result is a signed integer value equal
to the number of datepart boundaries crossed between the first and second date.
For example, the number of weeks between Sunday, January 4, and Sunday, January 11, is 1.
The number of months between March 31 and April 1 would be 1 because the month boundary
is crossed from March to April. The DATEDIFF function generates an error if the result is out of
range for integer values. For seconds, the maximum number is equivalent to approximately 68
years. The DATEDIFF function generates an error if a difference in weeks is requested and one
of the two dates precedes January 7 of the year 0001.

Examples of DATEDIFF

This function returns the value of 0 because no one-second boundaries are crossed.
DATEDIFF(SECOND, TIMESTAMP '2006-09-12 11:59:58.999998', TIMESTAMP '2006-09-12 11:59:58.999999')
This function returns the value 1 because a one-second boundary is crossed even though
the two timestamps differ by only one microsecond.
DATEDIFF(SECOND, TIMESTAMP '2006-09-12 11:59:58.999999', TIMESTAMP '2006-09-12 11:59:59.000000')
This function returns the value of 0.
DATEDIFF(YEAR, TIMESTAMP '2006-12-31 23:59:59.999998', TIMESTAMP '2006-12-31 23:59:59.999999')
This function returns the value of 1 because a year boundary is crossed.
DATEDIFF(YEAR, TIMESTAMP '2006-12-31 23:59:59.999999', TIMESTAMP '2007-01-01 00:00:00.000000')
This function returns the value of 2 because two WEEK boundaries are crossed.
DATEDIFF(WEEK, DATE '2006-01-01', DATE '2006-01-09')
This function returns the value of -29.
DATEDIFF(DAY, DATE '2004-03-01', DATE '2004-02-01')
"Datetime Value Expressions" (page
"Datetime Value Expressions" (page
210).
210).
DATEDIFF Function
321

Advertisement

Table of Contents
loading

Table of Contents