Calculate fiscal year in SQL Server

前端 未结 19 1480
时光说笑
时光说笑 2020-12-16 13:28

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

19条回答
  •  不思量自难忘°
    2020-12-16 13:55

    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')
    

提交回复
热议问题