我有一個需要以會計年度格式返回的日期字段。例如在sql select語句中計算會計年度?
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語句?
我有一個需要以會計年度格式返回的日期字段。例如在sql select語句中計算會計年度?
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語句?
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
David有一個很好的解決方案。一個更簡單的表達是:
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
我認爲這比CASE-WHEN-THEN-ELSE更簡單,但是在這個特殊的例子中你想要增加9個月。如果你增加9個月,4月將會漲到明年的1月份。 –
@CsabaToth。 。 。謝謝。我編輯了答案。 –
假設你有每年START_DATE表Fiscal_Year,和你想找到的財年給定日期@date:
SELECT MIN(Year) WHERE @Date >= Start_Date FROM Fiscal_Year
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
我只是選擇這樣做。這很容易,你可以用任何你想要的財政年度終結來代替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
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
...簡單的東西:)
(YEAR(DATEADD(月 - ((DATEPART(月,[日期])+ 5)%12),[日]) )+)AS Financial_Year
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是你的[起始日期]字段中,您可以:
select * from yourTable
group by convert(varchar(4),
case when MONTH([Start_Date]) < @FiscalYearStartMonth
then YEAR([Start_Date]) -1
else YEAR([Start_Date]) end)
希望這會有所幫助
這是oracle在當前財政年度看起來的樣子。輸入您想要查找會計年度以代替系統日期的日期
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;
因此您需要結束日期嗎? – dotjoe
是你輸入的數據是否正確,按照上面例子中的輸出?我認爲輸入是錯誤的。 – KumarHarsh