Calculate fiscal year in SQL Server

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

    Given @FiscalYearStartMonth is your fiscal year start month (numeric) and @Date is the date in question, do the following:

    SELECT 
      CASE 
          WHEN @FiscalYearStartMonth = 1 OR @FiscalYearStartMonth > MONTH(@Date) 
          THEN YEAR(@Date) 
          ELSE YEAR(@Date) + 1 
      END AS FiscalYear
    

    You can abstact this away in a function, or use as a column in a derived view

提交回复
热议问题