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

shieldscoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
Need version info

Each database vendor has a slightly different syntax for their SQL implementation. What database is this for please?
PortletPaulEE Topic AdvisorCommented:
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

shieldscoAuthor Commented:
java.sql.SQLSyntaxErrorException: ORA-00904: "RIGHT": invalid identifier
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

PortletPaulEE Topic AdvisorCommented:
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

PortletPaulEE Topic AdvisorCommented:
actually, using to_char() is probably safer/better than using substr() as it removes a implied type conversion

select 
      'FY ' || CASE WHEN EXTRACT(MONTH FROM C_COMPLETE_REQUEST_RECVD_DT) < 10 
                    THEN to_char(C_COMPLETE_REQUEST_RECVD_DT,'rr')
                    ELSE to_char(C_COMPLETE_REQUEST_RECVD_DT,'rr')
            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

Bill PrewIT / Software Engineering ConsultantCommented:
Please try this:

'FY ' || MOD(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,100) AS FY

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
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;
shieldscoAuthor Commented:
Thank you again Bill
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that helped.


»bp
awking00Information Technology SpecialistCommented:
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). :-(
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.