[英]current year's date of birth using oracle sql
Trying to get birth day date in the current year. 试图获取当年的出生日期。 For an example if DOB=19800925, birth day in the current year is 20180925 (note: we should not just replace the year because of the leap year DOB (ex: 20000229)) 例如,如果DOB = 19800925,则当前年份的出生日期为20180925(注意:我们不应该仅仅因为(年DOB而替换年份(例如:20000229))
with tmp_dob as
(
select '19900101' Birthday from dual union all
select '19901231' Birthday from dual union all
select '20040229' Birthday from dual union all
select '20041231' Birthday from dual union all
select '20171231' Birthday from dual union all
select '20051231' Birthday from dual
)
select Birthday,add_months(to_date(Birthday,'YYYYMMDD'),(trunc(months_between(sysdate ,to_date(Birthday,'YYYYMMDD'))/12)) * 12) current_year_dob
from tmp_dob;
This logic should work: 此逻辑应该起作用:
with tmp_dob as (
select to_date('19900101', 'YYYYMMDD') as Birthday from dual union all
select to_date('19901231', 'YYYYMMDD') Birthday from dual union all
select to_date('20040229', 'YYYYMMDD') Birthday from dual union all
select to_date('20041231', 'YYYYMMDD') Birthday from dual union all
select to_date('20171231', 'YYYYMMDD') Birthday from dual union all
select to_date('20051231', 'YYYYMMDD') Birthday from dual
)
select Birthday,
add_months(birthday, 12 * (extract(year from sysdate) - extract(year from birthday)))
from tmp_dob;
Add months approach works, but you'll get 2/28 for the leap-day birthdays. 增加几个月的方法效果,但是the日生日将得到2/28。 If you want that to be 03/01, or to be configurable, use a case to detect the condition and override: 如果希望将其设置为03/01或可配置,请使用案例来检测条件并覆盖:
with tmp_dob as (
select to_date('19900101', 'YYYYMMDD') as Birthday from dual union all
select to_date('19901231', 'YYYYMMDD') Birthday from dual union all
select to_date('20040229', 'YYYYMMDD') Birthday from dual union all
select to_date('20041231', 'YYYYMMDD') Birthday from dual union all
select to_date('20171231', 'YYYYMMDD') Birthday from dual union all
select to_date('20051231', 'YYYYMMDD') Birthday from dual
)
select Birthday,
case
when
to_date(extract(year from sysdate) || '1231', 'YYYYMMDD') -
to_date(extract(year from sysdate) || '0101', 'YYYYMMDD') < 365
and to_char(birthday, 'MMDD') = '0229' then
to_date(extract(year from sysdate) || '0301', 'YYYYMMDD')
else
add_months(birthday, 12 * (extract(year from sysdate) - extract(year from birthday)))
end as bd_this_year
from tmp_dob;
Results: 结果:
BIRTHDAY BD_THIS_YEAR
1/1/1990 1/1/2018
12/31/1990 12/31/2018
2/29/2004 3/1/2018
12/31/2004 12/31/2018
12/31/2017 12/31/2018
12/31/2005 12/31/2018
Consider making a function to do the same thing - your query will be easier to read. 考虑使函数执行相同的操作-您的查询将更易于阅读。
I thought about using an approach of strings since the birthdays you are using are on strings too. 我考虑过使用字符串的方法,因为您使用的生日也是字符串。 I use the BigQuery Syntax but there should be similar functions in every SQL. 我使用BigQuery语法,但每个SQL中应该都有类似的功能。
with tmp_dob as
(
select '19900101' Birthday from dual union all
select '19901231' Birthday from dual union all
select '20040229' Birthday from dual union all
select '20041231' Birthday from dual union all
select '20171231' Birthday from dual union all
select '20051231' Birthday from dual
)
select Birthday,
CASE WHEN MOD(CAST(SUBSTR(Birthday,1,4) AS INT64), 4) = 0 AND SUBSTR(Birthday,5,8) = '0229'
THEN CONCAT(CAST(EXTRACT(YEAR FROM CURRENT_DATE) AS STRING), '0228')
ELSE CONCAT(CAST(EXTRACT(YEAR FROM CURRENT_DATE) AS STRING), SUBSTR(Birthday,5,8)) END AS bd
from tmp_dob;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.