My Oracle Support Banner

RDBPROD: Date Arithmetic to Calculate Age in Years and Months (Doc ID 65210.1)

Last updated on AUGUST 03, 2016

Applies to:

Oracle Rdb Server on OpenVMS - Version 6.1 and later
HP OpenVMS VAX
HP OpenVMS Alpha
HP OpenVMS Itanium

Goal

Rdb date arithmetic operates on year and month portions.  Thus, performing the following SQL query yields the following:

 SQL> select BIRTHDAY, THISDAY, (THISDAY-BIRTHDAY) year as AGE from howoldami;

 BIRTHDAY     THISDAY         AGE
 1981-01-31   1991-01-30      10

1991 - 1981 = 10, however the birthday is still one day away, so the age is still nine years not ten.  

Extending the query to include month does not help in the case where the birthday is in the same month:

SQL> select BIRTHDAY, THISDAY, (THISDAY-BIRTHDAY) year to month as AGE from howoldami;

 BIRTHDAY     THISDAY         AGE
 1981-01-31   1991-01-30      10-00

The following example illustrates how to work around this restriction and return the correct age in years and months.

Solution

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.