Avatar of shieldsco
shieldsco
Flag for United States of America asked on

SQL Concatenate Fiscal Year

I using the code below and would like to concatenate the C_COMPLETE_REQUEST_RECVD_DT in the following format;
FY YY

Output:

FY 02
FY 14
FY 14
FY 15
FY 15
FY 15
FY 15
FY 15


,Case WHEN EXTRACT(MONTH FROM C_COMPLETE_REQUEST_RECVD_DT) < 10 THEN EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT) ELSE EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT)+1 END AS FY

Open in new window

Oracle DatabaseSQL

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
PortletPaul

Need version info

Each database vendor has a slightly different syntax for their SQL implementation. What database is this for please?
PortletPaul

select 
      concat('FY ', CASE WHEN EXTRACT(MONTH FROM C_COMPLETE_REQUEST_RECVD_DT) < 10 
                    THEN right(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT) ,2)
                    ELSE right(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT)+1 ,2)
            END) AS FY
from your_table

Open in new window

shieldsco

ASKER
java.sql.SQLSyntaxErrorException: ORA-00904: "RIGHT": invalid identifier
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
PortletPaul

OK. That's because I used a function supported by MySQL but not Oracle. You did not tell us the database was Oracle. In future please always add the database vendor to your sql related questions.

The alternative in Oracle is to use substr() and the manner of concatenation can be different as well.
select 
      'FY ' || CASE WHEN EXTRACT(MONTH FROM C_COMPLETE_REQUEST_RECVD_DT) < 10 
                    THEN substr(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT) ,-2)
                    ELSE substr(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT)+1 ,-2)
            END AS FY
from (
    select to_date('2017-09-11','yyyy-mm-dd') as C_COMPLETE_REQUEST_RECVD_DT from dual
    union all
    select to_date('2018-01-01','yyyy-mm-dd') as C_COMPLETE_REQUEST_RECVD_DT from dual
    ) d

OR

select 
      concat('FY ', CASE WHEN EXTRACT(MONTH FROM C_COMPLETE_REQUEST_RECVD_DT) < 10 
                    THEN substr(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT) ,-2)
                    ELSE substr(EXTRACT(YEAR FROM C_COMPLETE_REQUEST_RECVD_DT)+1 ,-2)
            END) AS FY
from (
    select to_date('2017-09-11','yyyy-mm-dd') as C_COMPLETE_REQUEST_RECVD_DT from dual
    union all
    select to_date('2018-01-01','yyyy-mm-dd') as C_COMPLETE_REQUEST_RECVD_DT from dual
    ) d

Open in new window

SOLUTION
PortletPaul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
awking00

I'm assuming you're trying to deal with a September 30th fiscal year ending. If that's the case, you might just try -
SELECT 'FY '||TO_CHAR(ADD_MONTHS(C_COMPLETE_REQUEST_RECVD_DT,3),'YY') as fiscal_year
FROM yourtable;
shieldsco

ASKER
Thank you again Bill
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Welcome, glad that helped.


»bp
awking00

I would have thought you might have found my solution worthwhile. It seemed a lot simpler than applying multiple extract functions, a case statement, and an implicit type conversion (i.e. adding 1 to the extract of the year before the concatenation). :-(