The following database view truncates the date to the fiscal year (April 1st):
CREATE OR REPLACE VIEW FISCAL_YEAR_VW AS
SELECT
CASE
WHEN to_number(to_char(SYSDATE, 'MM')) < 4 THEN
to_date('1-APR-'||to_char(add_months(SYSDATE, -12), 'YYYY'), 'dd-MON-yyyy')
ELSE
to_date('1-APR-'||to_char(SYSDATE, 'YYYY'), 'dd-MON-yyyy')
END AS fiscal_year
FROM
dual;
This allows us to calculate the current fiscal year based on today's date.
How can this calculation be simplified or optimized?
TRUNC() can be usefully applied to dates, with different format masks. Most pertinently,
trunc(sysdate, 'yyyy')
gives us the first day of the year. So this will give us the 01-APR of the current year ...and this that date for the previous year ...
So:
caveat: I haven't had the chance to test this code yet so it might contain typos. I will test it later and correct it if necessary.
Output will be:
I find the TO_CHAR(date, 'Q') feature of oracle very useful for calculating fiscal calendars. The query below uses the 'with' clause to build two things
Example:
Output:
Perhaps this...
I guess this is another option...
Other options are rewriting as a function that returns a date, or the logic could be simplified if you could just return the year number for the current fiscal year since you'd only need the logic within the to_char.