我需要使用日期格式和系统日期中的列Dob将出生日期转换为当前年龄,但是将结果填充到dob列中。 日期格式为dd-mm-yy。 我已经尝试使用to_char和to_number函数从sys日期中减去
答案 0 :(得分:2)
假设您只需要多年且dob
列为date
,您可能需要:
select dob, floor(months_between(sysdate, dob) / 12) age
from yourTable
例如:
with yourTable(dob) as (
select date '2000-02-01' from dual union all
select date '2000-02-02' from dual union all
select date '2000-01-31' from dual
)
select dob, floor(months_between(sysdate, dob) / 12) age
from yourTable
给出:
DOB AGE
---------- ----------
2000-02-01 18
2000-02-02 17
2000-01-31 18
3 rows selected.
答案 1 :(得分:0)
如果您需要date_of_birth列的年,月和日。你可以这样做。
select sysdate,
date_of_birth,
trunc(months_between(sysdate,date_of_birth) / 12) as years,
trunc(months_between(sysdate,date_of_birth) -
(trunc(months_between(sysdate,date_of_birth) / 12) * 12)) as months,
trunc(sysdate) - add_months(date_of_birth, trunc(months_between(sysdate,date_of_birth))) as days
from table_name;