Calculate fiscal year in SQL Server

前端 未结 19 1508
时光说笑
时光说笑 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:48

    I've extended the answer posted by ChrisF and Conficker.

    DECLARE @FFYStartMonth INT = 10 --The first month of the FFY
    DECLARE @EntryDate DATETIME = '4/1/2015' --The date of the data
    DECLARE @StartDate DATETIME
    
    DECLARE @EndDate DATETIME
    
    SET @StartDate = DATEADD(dd, 0,
        DATEDIFF(dd, 0,
            DATEADD(mm, - (((12 + DATEPART(m, @EntryDate)) - @FFYStartMonth)%12), @EntryDate) -
    datePart(d,DATEADD(mm, - (((12 + DATEPART(m, @EntryDate)) - @FFYStartMonth )%12),
        @EntryDate )) + 1 ))  
    
    SET @EndDate = DATEADD(SS, -1, DATEADD(mm, 12, @StartDate))
    
    SELECT @StartDate, @EndDate
    

提交回复
热议问题