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

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

TIMESTAMPDIFF Function

The TIMESTAMPDIFF function returns the integer value for the number of interval-ind
units of time between startdate and enddate. If enddate precedes startdate, the return
value is negative or zero.
TIMESTAMPDIFF (interval-ind, startdate, enddate)
interval-ind
is SQL_TSI_YEAR, SQL_TSI_MONTH, SQL_TSI_DAY, SQL_TSI_HOUR, SQL_TSI_MINUTE,
SQL_TSI_SECOND, SQL_TSI_QUARTER, or SQL_TSI_WEEK
startdate and enddate
are each of type DATE or TIMESTAMP
The method of counting crossed boundaries such as days, minutes, and seconds makes the result
given by TIMESTAMPDIFF consistent across all data types. The TIMESTAMPDIFF function
makes these boundary assumptions:
A year begins at the start of January 1.
A new quarter begins on January 1, April 1, July 1, and October 1.
A week begins at the start of Sunday.
A day begins at midnight.
The result is a signed integer value equal to the number of interval-ind 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 TIMESTAMPDIFF 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 TIMESTAMPDIFF
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 TIMESTAMPDIFF

This function returns the value 1 because a 1-second boundary is crossed even though the
two timestamps differ by only one microsecond:
TIMESTAMPDIFF (SQL_TSI_SECOND, TIMESTAMP '2006-09-12 11:59:58.999999',
TIMESTAMP '2006-09-12 11:59:59.000000')
This function returns the value 0 because no 1-second boundaries are crossed:
TIMESTAMPDIFF (SQL_TSI_YEAR, TIMESTAMP '2006-12-31 23:59:59.00000,
TIMESTAMP '2006-12-31 23:59:59.999999')
This function returns the value 1 because a year boundary is crossed:
TIMESTAMPDIFF (SQL_TSI_YEAR, TIMESTAMP '2006-12-31 23:59:59.999999',
TIMESTAMP '2007-01-01 00:00:00.000000;)
This function returns the value 1 because a WEEK boundary is crossed:
TIMESTAMPDIFF (SQL_TSI_WEEK, DATE '2006-01-01', DATE '2006-01-09')
This function returns the value of -29:
TIMESTAMPDIFF (SQL_TSI_DAY, DATE '2004-03-01', DATE '2004-02-01')
478
SQL Functions and Expressions

Advertisement

Table of Contents
loading

Table of Contents