DB2 Scalar functions - AGE


Volta a página anterior

Volta ao Menu das scalar functions

Volta ao Menu Principal


Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

AGE

The AGE function returns a numeric value that represents the number of full years, full months, and full days between the current timestamp and the argument.

AGE(expression)

The schema is SYSIBM.

expression
An expression that specifies the datetime value for which the age is computed.
The expression must return a value that is a DATE, TIMESTAMP, CHAR, or VARCHAR data type.
In a Unicode database, the expression can also be a GRAPHIC or VARGRAPHIC data type.
CHAR, VARCHAR, GRAPHIC, and VARGRAPHIC are supported by using implicit casting.
If expression is a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it must be a valid string that is accepted by the TIMESTAMP scalar function

If there is less than a full day between the current timestamp and expression, the result is zero.
If expression is earlier than the current timestamp, the result is positive.
If expression is later than the current timestamp, the result is negative.

The result of the function is an INTEGER. If the argument can be null, the result can be null.
If the argument is null, the result is the null value.

The AGE function is a synonym of the following expression:

INTEGER( ( CURRENT TIMESTAMP(12) - TIMESTAMP( expression, 12 ) ) / 1000000 )

The result is the integer representation of the extraction of the year, month, and day components of a timestamp duration.

Notes

  • Determinism: AGE is a deterministic function.
    However, the invocation of the function depends on the value of the special register CURRENT TIMESTAMP.
    The AGE function can be used wherever special registers are supported (SQLSTATE 42621, 428EC, or 429BX).

Examples

  1. Assume the CURRENT TIMESTAMP(12) is 2013-09-24-11.28.00.123456789012.
    Set the host variable AGE1 to the number of full years, full months, and full days between the current timestamp and 2012-02-28-12.00.00.
    SET :AGE1 = AGE(TIMESTAMP '2012-02-28-12.00.00')
    The host variable AGE1 is set to 10624.

  2. Assume the CURRENT TIMESTAMP(12) is 2013-09-24-11.28.00.123456789012.
    Set the host variable AGE1 to the number of full years, full months, and full days between the current timestamp and 2013-09-23-12.00.00.
    SET :AGE1 = AGE(TIMESTAMP '2013-09-23-12.00.00')
    The host variable AGE1 is set to 0.

  3. Assume the CURRENT TIMESTAMP(12) is 2013-09-24-11.28.00.123456789012.
    Set the host variable AGE1 to the number of full years, full months, and full days between the current timestamp and 2020-01-01.
    SET :AGE1 = AGE(DATE '2020-01-01')
    The host variable AGE1 is set to -60306.

© Copyright IBM Corp.