Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate fiscal year in SQL Server

How would you calculate the fiscal year from a date field in a view in SQL Server?

like image 980
R0b0tn1k Avatar asked Nov 20 '09 17:11

R0b0tn1k


People also ask

How do I find the fiscal year in SQL Server?

This is a way to get current financial year using SQL query: DECLARE @FIYear VARCHAR(20) SELECT @FIYear = (CASE WHEN (MONTH(GETDATE())) <= 3 THEN convert(varchar(4), YEAR(GETDATE())-1) + '-' + convert(varchar(4), YEAR(GETDATE())%100)

How is fiscal year calculated?

According to the IRS, a fiscal year consists of 12 consecutive months ending on the last day of any month except December. 1 Alternatively, instead of observing a 12-month fiscal year, U.S. taxpayers may observe a 52- to 53-week fiscal year.

How do you find the financial year in SQL?

actually i calculate financial year like below in VB code. i just like the same in sql server query. "select * from admission where std_code=1 and fnyr in (select finy in system date)"<---i need here. Thanking you.

How is fiscal month calculated in SQL?

SQL does not really store "Partial Dates" (Month/Year without Day/Time). Since your fiscal month is always three months ahead of the calendar month, a calculated column will work well here.


1 Answers

I suggest you use a User-Defined Function based on the Fiscal year of your application.

CREATE FUNCTION dbo.fnc_FiscalYear(
    @AsOf           DATETIME
)
RETURNS INT
AS
BEGIN

    DECLARE @Answer     INT

    -- You define what you want here (September being your changeover month)
    IF ( MONTH(@AsOf) < 9 )
        SET @Answer = YEAR(@AsOf) - 1
    ELSE
        SET @Answer = YEAR(@AsOf)


    RETURN @Answer

END



GO

Use it like this:

SELECT dbo.fnc_FiscalYear('9/1/2009')


SELECT dbo.fnc_FiscalYear('8/31/2009')
like image 186
Brett Veenstra Avatar answered Sep 29 '22 20:09

Brett Veenstra