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?
 
Bill PrewConnect With a Mentor Commented:
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
0
 
PortletPaulfreelancerCommented:
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

0
 
shieldscoAuthor Commented:
java.sql.SQLSyntaxErrorException: ORA-00904: "RIGHT": invalid identifier
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
PortletPaulfreelancerCommented:
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

0
 
PortletPaulConnect With a Mentor freelancerCommented:
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

0
 
awking00Commented:
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;
1
 
shieldscoAuthor Commented:
Thank you again Bill
0
 
Bill PrewCommented:
Welcome, glad that helped.


»bp
0
 
awking00Commented:
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). :-(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.