在sql select语句中计算财务年度?

时间:2013-12-12 21:27:06

标签: sql sql-server tsql

我有一个日期字段,需要以会计年度格式返回。示例

Start_Date        Year 
04/01/2012 -      2013
01/01/2012 -      2012
09/15/2013 -      2014

我们需要计算

04/01/2012 to 03/31/2013 is FY 2013

04/01/2013 to 03/31/2014 is FY 2014

我们如何在select语句中执行此操作?

10 个答案:

答案 0 :(得分:12)

大卫有一个非常好的解决方案。更简单的表达方式是:

select year(dateadd(month, -3, start_date)) as FiscalYear

即减去3个月并取一年。

编辑:

正如评论中所指出的,这似乎过早产生了一年。这有两个解决方案:

select year(dateadd(month, 9, start_date)) as FiscalYear

select 1 + year(dateadd(month, -3, start_date)) as FiscalYear

答案 1 :(得分:10)

SELECT CASE WHEN DatePart(Month, Start_Date) >= 4
            THEN DatePart(Year, Start_Date) + 1
            ELSE DatePart(Year, Start_Date)
       END AS Fiscal_Year
FROM data

答案 2 :(得分:1)

我只是选择这样做。这很容易,你可以用你想要的财政年度结束来取代09,30。不确定这是否适用于除SQL Server之外的任何内容。

CASE 
    WHEN CAST(GETDATE() AS DATE) > 
         SMALLDATETIMEFROMPARTS(DATEPART(YEAR,GETDATE()),09,30,00,000) 
    THEN 
        DATEPART(YEAR,GETDATE()) + 1 ELSE DATEPART(YEAR,GETDATE()) 
    END AS FY

答案 3 :(得分:0)

假设您有一个表Fiscal_Year,每年的Start_Date,并且您希望找到给定日期@Date的会计年度:

SELECT MIN(Year) WHERE @Date >= Start_Date FROM Fiscal_Year

答案 4 :(得分:0)

Declare @t table(StartDate date ,Year1 int)
insert into @t values('04/01/2012',2013),('01/01/2012',2012),('09/15/2013',2014)
;with CTE as
(select max(year1) maxyear from @t)
, cte1 as
 (select cast('04/01/'+convert(varchar(4),a.year1) as  date) FromFY,dateadd(day,-1,dateadd(year,1,cast('04/01/'+convert(varchar(4),a.year1) as date))) ToFY


,b.year1 from @t a  inner join (select min(year1)year1 from @t) b on a.year1=b.year1
    union all
   select cast(dateadd(day,1,ToFY) as date),cast(dateadd(year,1,ToFY) as date),year1+1 year1 from     cte1 where year1<=(select maxyear from cte)
)

select * from cte1

答案 5 :(得分:0)

DECLARE @STARTDATE DATETIME, @ENDDATE DATETIME,@CURR_DATE DATETIME
SET @CURR_DATE='2015-01-30'
IF MONTH(@CURR_DATE) IN (1,2,3)
BEGIN
    SET @STARTDATE= CAST( CAST(YEAR(@CURR_DATE)-1 AS VARCHAR)+'/04/01'  AS DATE)
    SET @ENDDATE= CAST( CAST(YEAR(@CURR_DATE)  AS VARCHAR)+'/03/31'  AS DATE)
END
ELSE
BEGIN
    SET @STARTDATE= CAST( CAST(YEAR(@CURR_DATE) AS VARCHAR)+'/04/01'  AS DATE)
    SET @ENDDATE= CAST( CAST(YEAR(@CURR_DATE)+1 AS VARCHAR)+'/03/31'  AS DATE)
END
SELECT @STARTDATE AS ST_FI,@ENDDATE AS END_FY

答案 6 :(得分:0)

......简单的东西:)

(年(DATEADD(月, - ((DATEPART(月,[日期])+ 5)%12),[日期]))+)AS Financial_Year

答案 7 :(得分:0)

declare @Date smalldatetime, @FiscalYearStartMonth tinyint
set @Date = GETDATE();
set @FiscalYearStartMonth = 1; -- Jan

print convert(varchar(4), 
case when MONTH(@Date) < @FiscalYearStartMonth 
then YEAR(@Date) -1 
else YEAR(@Date) end)

假设@Date是您的[Start_Date]字段,您可以:

select * from yourTable 
group by convert(varchar(4), 
case when MONTH([Start_Date]) < @FiscalYearStartMonth 
then YEAR([Start_Date]) -1 
else YEAR([Start_Date]) end)

希望这有帮助

答案 8 :(得分:0)

这是oracle在当前财政年度的样子。输入您要查找会计年度的日期以代替sysdate

c

答案 9 :(得分:0)

仅年份格式(2020财年),请使用以下查询:

SELECT tender_opening_date,CASE WHEN MONTH( order_date) >= 4
            THEN CONCAT('FY ',YEAR(( order_date)) +1  )
            ELSE CONCAT('FY ',YEAR( order_date)-1   )
       END AS Fiscal_Year
FROM orders_tbl

对于完整的财政年度(2019-20财年),请使用以下查询:

SELECT tender_opening_date,CASE WHEN MONTH( order_date) >= 4
                THEN CONCAT('FY ',YEAR( order_date),'-',DATE_FORMAT( order_date,'%y') +1  )
                ELSE CONCAT('FY ',YEAR( order_date)-1,'-',DATE_FORMAT( order_date,'%y')   )
           END AS Fiscal_Year
    FROM orders_tbl