Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get year and Quarter

Ram venuJun 27 2019 — edited Jun 28 2019

I am written below code to get prev quarter and year ,

if current date is in 1st quarter , I have to get previous quarter (last year last quarter)  , same time year has to be previous year.

if current quarter is 2 then previous quarter is 1 and year is current year ,

can we write below in simplified way ?

please help

SELECT

CASE WHEN TO_NUMBER(to_char(TO_DATE('04-01-2018','MM-DD-YYYY'),'Q')) -1=0

    THEN extract(year from sysdate)-1 else  extract(year from sysdate) end year ,

CASE WHEN TO_NUMBER(to_char(TO_DATE('04-01-2018','MM-DD-YYYY'),'Q')) -1= 0 then 4 else 

      TO_NUMBER(to_char(TO_DATE('04-01-2018','MM-DD-YYYY'),'Q')) -1 end last_qtr

FROM dual;

This post has been answered by mathguy on Jun 27 2019
Jump to Answer
Comments
Post Details
Added on Jun 27 2019
2 comments
2,164 views