简体   繁体   中英

How to calculate age using DOB in oracle sql?

How to calculate age using DOB in oracle sql? Column name: GET_DOB Table name: J, DOB Date format: 8/10/2022

Assuming your DOB column is a date you could do the following:

SELECT a.*, TRUNC(MONTHS_BETWEEN(sysdate, dob) / 12) age
FROM   your_table a

otherwise you need to cast string to date:

SELECT a.*, TRUNC(MONTHS_BETWEEN(sysdate, TO_DATE(dob, 'DD/MM/YYYY')) / 12) age
FROM   your_table a

In case you want to have age as a column (bear in mind that age changes with time):

CREATE VIEW your_view
AS
  SELECT a.*,
         TRUNC(MONTHS_BETWEEN(sysdate, dob) / 12) age
  FROM   your_table a; 

Use this SQL fiddle for reference: http://sqlfiddle.com/#!4/c8e1b/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM