I have a month and day; I’m trying to work out the next date that this month and day will exist after a given date.
For instance if I have a table as follows, where the reference_date is of the format 'MMDD'. Please don’t blame me for this; it is the official format for filing in the UK.
create table tmp_ben_dates ( filing_date date, reference_date varchar2(4));
insert all
into tmp_ben_dates values ( to_date('31/12/2011','dd/mm/yyyy'), '1231')
into tmp_ben_dates values ( to_date('31/12/2011','dd/mm/yyyy'), '1130')
into tmp_ben_dates values ( to_date('31/12/2011','dd/mm/yyyy'), '0101')
into tmp_ben_dates values ( to_date('31/07/2011','dd/mm/yyyy'), '0601')
into tmp_ben_dates values ( to_date('31/07/2011','dd/mm/yyyy'), '0801')
select * from dual;
I would like to return the first date that each reference_date occurs after each filing_date. For instance in the first example this would be 31/12/2012 and in the last this would be 01/08/2011.
My best attempt, so far, is as follows
with new_date as (
select reference_date
, filing_date
, add_months( trunc(filing_date,'y')
, to_number(substr(reference_date,1,2)) - 1)
+ to_number(substr(reference_date,3)) - 1 as the_date
from tmp_ben_dates
)
select filing_date
, reference_date
, case when filing_date < the_date then next_date
else add_months(the_date,12) end
from new_date
Which returns the correct results:
FILING_DATE REFE NEXT_DATE
------------------- ---- -------------------
31/12/2011 00:00:00 1231 31/12/2012 00:00:00
31/12/2011 00:00:00 1130 30/11/2012 00:00:00
31/12/2011 00:00:00 0101 01/01/2012 00:00:00
31/07/2011 00:00:00 0601 01/06/2012 00:00:00
31/07/2011 00:00:00 0801 01/08/2011 00:00:00
However, it’s also completely ridiculous; difficult to read and understand.
It’s also possible using interval but I don’t really see how it makes it any less confusing to determine what’s going on.
with new_date as (
select reference_date
, filing_date
, trunc(filing_date,'y')
+ to_yminterval( 'P'
|| to_char(to_number(substr(reference_date,1,2)) - 1)
|| 'M')
+ to_dsinterval( 'P'
|| to_char(to_number(substr(reference_date,3)) - 1)
|| 'D') as the_date
from tmp_ben_dates
)
select filing_date
, reference_date
, case when filing_date < the_date then the_date
else add_months(the_date,12) end as next_date
from new_date
Is there something really obvious that I’m missing here? Is there an easy method of doing this in SQL?
You might find this simpler. It simply puts the date in YYYYMMDD format and uses that as the second argument to to_date():