-6

How can I calculate date of birth from an age field in Oracle Apex?

I created a dynamic action to execute server side code on date of birth filed to calculate age from date of birth:

select trunc(months_between(sysdate,:P25_DATE_OF_BIRTH)/12) ||'.'||
trunc(mod(months_between(sysdate,:P25_DATE_OF_BIRTH),12))
into :P25_AGE from dual;

Now I need to create a dynamic action on the age field to calculate date of birth given age, for example age 43.4.

10
  • 7
    How do you define an age of 43.4? Even if you say, 43 + 365 * 0.4 - who on earth is able to provide his age like this? Commented May 17 at 15:54
  • 1
    If someone told me their age was 43.4, I’d ask them if they were born yesterday. Commented May 17 at 17:42
  • 2
    Still not clear. I am 53 years old. If I tell you this, then my birthday can be between 1971-05-20 and 1972-05-18 - how can you know? Commented May 19 at 6:31
  • 2
    @Abdullah when you subtract your current age from the current year, then you a) still need to know whether you already had your birthday this year, and b) then only have a birth YEAR, not a birth DATE as requested in your question Commented May 19 at 13:25
  • 2
    @Abdullah: It seems you haven't read what Wernfried illustrated: You cannot determine the year of birth from the age. In Wernfrieds example, being 53 now, he could have been born in 1971 or 1972. Both years are possible. Commented May 19 at 20:15

2 Answers 2

4

A year is 365.2422 days long. So, a person at age 43.4 is 15851.51148 days old.

Now we can subtract those days from the current time and get the date and even the time the person was born:

select systimestamp - interval '1' day * 365.2422 * 43.4

So, if a person told me today at May 17, 2025 at 19:00 that they were 43.4 years old, I would know they were born on December 23, 1981 at 06:43.

Sign up to request clarification or add additional context in comments.

2 Comments

how can I get number of years only for example age = 30 years , I need to subtract sysdate - 30 years and get the year of birth , 2025 - 30 = 1995 ?
If you are 30 years old now, but have your (31st) birthday after today (may 2025), then you were born in 1994, not 1995
0

Your comments indicate that you're after the year of birth, not the birthdate.

This can be done using the sql function ADD_MONTHS

Both allow you to add/substract a number of years from sysdate. Example:

SELECT add_months( sysdate ,- 12 * 30 ) FROM dual;

To get the actual year, just use a TO_CHAR with format mask 'YYYY' like this:

SELECT TO_CHAR(add_months( sysdate ,- 12 * 30 ),'YYYY') FROM dual;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.