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

  • SEARCH
  • Home
  • 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 9041037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:08:24+00:00 2026-06-16T10:08:24+00:00

I am trying to write a query that will give me the max date

  • 0

I am trying to write a query that will give me the max date form Table_A based on the following condition.

If the given date (input parameter) falls between 01 April and 30 Sept of that year then return MAX(date) prior to April 1.

OR

If the given date (input parameter) falls between 01 Oct and 31 March of the following year then return MAX(date) prior to Oct 1.

For example :

  1. Given date is 27th Dec, 2012 (date between 01 Oct and 31 March of the following year)
    return 29th Sept, 2012
  2. Given Date is 17th Aug, 2012 (date between 01 April and 30 Sept of the following year)
    return date (MAX DATE prior to is 01 April is 29th March)
  • 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-16T10:08:25+00:00Added an answer on June 16, 2026 at 10:08 am

    try this:

    select max(case
                when trunc(the_date) between to_date('01-apr-'||the_year, 'dd-mon-yyyy')
                                         and to_date('30-sep-'||the_year, 'dd-mon-yyyy')
                 and dte < to_date('01-apr-'||the_year, 'dd-mon-yyyy')
                then
                  dte
                when trunc(the_date) between to_date('01-oct-'||the_year, 'dd-mon-yyyy')
                                         and to_date('31-mar-'||(the_year+1), 'dd-mon-yyyy')
                 and dte < to_date('01-oct-'||the_year, 'dd-mon-yyyy')
                then
                  dte
               end) max_date
      from table_a a 
           cross join (select v_inp_date the_date,
                              to_char(add_months(v_inp_date,-3), 'yyyy') the_year
                         from dual) dte;
    

    so you input date is in the dte part

    v_inp_date
    

    and

     to_char(add_months(v_inp_date,-3), 'yyyy') the_year
    

    eg a small test:

    SQL> create table table_a(dte date);
    
        Table created.
    
    SQL> insert into table_a values(to_date('28-sep-2012', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> insert into table_a values(to_date('29-sep-2012', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> insert into table_a values(to_date('28-mar-2012', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> insert into table_a values(to_date('29-mar-2012', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> insert into table_a values(to_date('28-sep-2011', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> insert into table_a values(to_date('27-mar-2011', 'dd-mon-yyyy'));
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> var inpdate varchar2(20);
    SQL> exec :inpdate := '27-dec-2012';
    
    PL/SQL procedure successfully completed.
    
    SQL> select max(case
      2              when trunc(the_date) between to_date('01-apr-'||the_year, 'dd-mon-yyyy')
      3                                       and to_date('30-sep-'||the_year, 'dd-mon-yyyy')
      4               and dte < to_date('01-apr-'||the_year, 'dd-mon-yyyy')
      5              then
      6                dte
      7              when trunc(the_date) between to_date('01-oct-'||the_year, 'dd-mon-yyyy')
      8                                       and to_date('31-mar-'||(the_year+1), 'dd-mon-yyyy')
      9               and dte < to_date('01-oct-'||the_year, 'dd-mon-yyyy')
     10              then
     11                dte
     12             end) max_date
     13    from table_a a
     14         cross join (select to_date(:inpdate,'dd-mon-yyyy') the_date,
     15                            to_char(add_months(to_date(:inpdate,'dd-mon-yyyy'), -3), 'yyyy') the_year
     16                       from dual) dte;
    
    MAX_DATE
    ---------
    29-SEP-12
    
    SQL> exec :inpdate := '17-aug-2012';
    
    PL/SQL procedure successfully completed.
    
    SQL> /
    
    MAX_DATE
    ---------
    29-MAR-12
    
    SQL> exec :inpdate := '01-oct-2011';
    
    PL/SQL procedure successfully completed.
    
    SQL> /
    
    MAX_DATE
    ---------
    28-SEP-11
    
    SQL> exec :inpdate := '01-apr-2011';
    
    PL/SQL procedure successfully completed.
    
    SQL> /
    
    MAX_DATE
    ---------
    27-MAR-11
    
    SQL>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that will return data grouped by date ranges
I'm trying to write a query that will: Run a query, give me (x)
I am trying to write a query that will select rows based on the
I'm trying to write a query that will set a bit value, based on
I am trying to write a query that will return the 'Audit Date' field
I'm trying to write a query that will check today's date against my table
I'm trying to write a query that will return the closest match from a
I am trying to write a linq query that will only return certain columns
I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that
I'm trying to write a query that will return all QUERY_ID values alongside all

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.