calculate fiscal year in sql select statement?

后端 未结 10 1319
谎友^
谎友^ 2020-12-19 05:10

I have a date field that needs to return in fiscal year format. example

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


        
相关标签:
10条回答
  • 2020-12-19 05:51

    Assuming that you have a table Fiscal_Year with Start_Date per Year, and you want to find the fiscal year for a given date @Date:

    SELECT MIN(Year) WHERE @Date >= Start_Date FROM Fiscal_Year
    
    0 讨论(0)
  • 2020-12-19 05:51

    For only year format (FY 2020) use following query:

    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
    

    For full financial Year (FY 2019-20) use following query:

    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
    
    0 讨论(0)
  • 2020-12-19 06:03
    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
    
    0 讨论(0)
  • 2020-12-19 06:03
    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)
    

    assuming @Date is your [Start_Date] field, you can:

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

    Hope this helps

    0 讨论(0)
  • 2020-12-19 06:05

    ...something simple :)

    (YEAR(DATEADD(Month,-((DATEPART(Month,[Date])+5) %12),[Date]))+) AS Financial_Year

    0 讨论(0)
  • 2020-12-19 06:07

    This is what would look like in oracle to get current financial year. Enter the date for which you want to find fiscal year in place of sysdate

     SELECT '01-APR-'
     ||TO_CHAR((add_months(**sysdate**,-3)),'YYYY') FIN_YEAR_START_DATE,
     '31-MAR-'
     ||(TO_CHAR((add_months(**sysdate**,-3)),'YYYY')+1) FIN_YEAR_END_DATE
     FROM dual;
    
    0 讨论(0)
提交回复
热议问题