SQL Concatenate Fiscal Year

shieldsco
shieldsco used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
Need version info

Each database vendor has a slightly different syntax for their SQL implementation. What database is this for please?
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

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

Author

Commented:
java.sql.SQLSyntaxErrorException: ORA-00904: "RIGHT": invalid identifier
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
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 Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
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

Test your restores, not your backups...
Top Expert 2016
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
awking00Information Technology Specialist

Commented:
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;

Author

Commented:
Thank you again Bill
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome, glad that helped.


»bp
awking00Information Technology Specialist

Commented:
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). :-(

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial