简体   繁体   中英

Extract Week of Fiscal year from Date ORACLE SQL

My Fiscal Year start from 1st july to 30 June.I want to calculate weeks from start of Financial year for a year. WEEK1,WEEK2 WEEK3...WEEK 52 ETC

Following will give you week number in a number datatype

to_number(to_char(to_date('07/01/2019','MM/DD/YYYY'),'WW')) as week_num

and if you want week based on ISO standard the use following

to_number(to_char(to_date('07/01/2019','MM/DD/YYYY'),'IW')) as week_num

Try this:

FLOOR(theDate - TO_DATE('07/01/2019','MM/DD/YYYY')) / 7) + 1 AS FISCAL_WEEK

where theDate is a date between 07/01/2019 and 06/30/2020.

You can use conditional logic:

select (case when extract(month from sysdate) < 7
             then floor((sysdate - trunc(sysdate, 'YYYY')) / 7) + 1
             else floor((sysdate - (trunc(sysdate, 'YYYY') + interval '6' month))/7) + 1
        end) as fiscal_week

This uses sysdate to represent the date. You can, of course, replace it with your date column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM