Oracle计算年龄:年、月、天
oracle 数据库计算年龄:年、月、日计算规则:1. 计算年生日已过则:当前年-出生年生日未过则:当前年-出生年-12. 计算月:当前月>=出生月 and 当前日>=出生日则:当前月-出生月当前月>=出生月 and 当前日<出生日则:当前月-出生月-1当前月<出生月 and 当前日>=出生日则:12-出生月+当前月当前月>=出生月 and 当前日<
·
oracle 数据库计算年龄:年、月、日
计算规则:
1. 计算年
生日已过
则:当前年-出生年
生日未过
则:当前年-出生年-1
2. 计算月:
当前月>=出生月 and 当前日>=出生日
则:当前月-出生月
当前月>=出生月 and 当前日<出生日
则:当前月-出生月-1
当前月<出生月 and 当前日>=出生日
则:12-出生月+当前月
当前月>=出生月 and 当前日<出生日
则:12-出生月+当前月-1
3. 计算日:
当前日>=出生日
则:当前日-出生日
当前日<出生日
则:出生日当月总天数-出生日+当前日
select
BIRTHDAY as "生日",
(case when to_char(sysdate,'mmdd')>=to_char(BIRTHDAY,'mmdd') --生日已过
then to_char(sysdate,'yyyy')-to_char(BIRTHDAY,'yyyy')
else to_char(sysdate,'yyyy')-to_char(BIRTHDAY,'yyyy')-1 --生日未过
end ) as "年",
(case when to_char(sysdate,'mm')>=to_char(BIRTHDAY,'mm') and to_char(sysdate,'dd')>=to_char(BIRTHDAY,'dd')
then to_char(sysdate,'mm')-to_char(BIRTHDAY,'mm')
when to_char(sysdate,'mm')>=to_char(BIRTHDAY,'mm') and to_char(sysdate,'dd')<to_char(BIRTHDAY,'dd')
then to_char(sysdate,'mm')-to_char(BIRTHDAY,'mm')-1
when to_char(sysdate,'mm')<to_char(BIRTHDAY,'mm') and to_char(sysdate,'dd')>=to_char(BIRTHDAY,'dd')
then 12 - to_char(BIRTHDAY,'mm') + to_char(sysdate,'mm')
when to_char(sysdate,'mm')<to_char(BIRTHDAY,'mm') and to_char(sysdate,'dd')>=to_char(BIRTHDAY,'dd')
then 12 - to_char(BIRTHDAY,'mm') + to_char(sysdate,'mm')-1
end) as "月",
(case when to_char(sysdate,'dd')>=to_char(BIRTHDAY,'dd')
then to_char(sysdate,'dd')-to_char(BIRTHDAY,'dd')
else to_char(last_day(BIRTHDAY),'dd')-to_char(BIRTHDAY,'dd')+to_char(sysdate,'dd')
end
) as "天数"
from Pi_MASTER
更多推荐
已为社区贡献1条内容
所有评论(0)