Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:43:03+00:00 2026-06-09T15:43:03+00:00

I have a month and day; I’m trying to work out the next date

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:43:05+00:00Added an answer on June 9, 2026 at 3:43 pm

    You might find this simpler. It simply puts the date in YYYYMMDD format and uses that as the second argument to to_date():

    with new_date as (
        select reference_date, filing_date,
               to_date(to_char(filing_date, 'yyyy')||reference_date), 'yyyymmdd'
                      ) - 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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a string that is like this 2012/10/12 10:03:46 (Year/Month/Day) format now i
I have a route defined like this: routes.MapRoute(Date, Date/{year}/{month}/{day}, new { controller = Date,
I have this data that I'd like to save to plist. MONTH and DAY
I have the day,month and year of a date.I have found that php has
I have a directory full of log files in the form ${name}.log.${year}{month}${day} such that
I have a form which posts date information month, day, yeah, hour, minute, am/pm.
Say I have the following code: struct date { int day; int month; int
I have the below code, $lastmonthLastDay = date('Y-m-d', strtotime(last day of last month)); $lastmonthFirstDay=
I have a database that stores dates broken into int's. I.e. Day, Month and
I have an array with a series of event IDs organized like this: $event['year']['month']['day']

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.