How would you calculate the fiscal year from a date field in a view in SQL Server?
CASE
WHEN MONTH(Date) > 6
THEN YEAR(Date) + 1
ELSE YEAR(Date)
END AS [FISCAL YEAR]
In this case, Fiscal Year starts on 7/1. This is the simplest solution out there.
Here's my version which returns fiscal year as FYyyyy - fiscal year begins 7/1
i.e. 6/1/2015 -> FY1415, 7/1/2015 -> FY1516
String functions could be better...
CREATE FUNCTION [dbo].[FY](@DATE DATETIME)
RETURNS char(6)
AS
BEGIN
DECLARE @Answer char(6)
SET @Answer =
CASE WHEN MONTH(@DATE) < 7
THEN 'FY' + RIGHT(CAST(YEAR(@DATE) - 1 AS VARCHAR(11)), 2) + RIGHT(CAST(YEAR(@DATE) AS VARCHAR(11)), 2)
ELSE 'FY' + RIGHT(CAST(YEAR(@DATE) AS VARCHAR(11)), 2) + RIGHT(CAST(YEAR(@DATE) + 1 AS VARCHAR(11)), 2) END
RETURN @Answer
END
declare
@InputDate datetime,
@FiscalInput varchar(2),
@FiscalYear varchar(4),
@FiscalMonth varchar(2),
@FiscalStart varchar(10),
@FiscalDate varchar(10)
set @FiscalInput = '10'
set @InputDate = '1/5/2010'
set @FiscalYear = (select
case
when datepart(mm,@InputDate) < cast(@FiscalInput as int)
then datepart(yyyy, @InputDate)
when datepart(mm,@InputDate) >= cast(@FiscalInput as int)
then datepart(yyyy, @InputDate) + 1
end FiscalYear)
set @FiscalStart = (select @FiscalInput + '/01/' + @FiscalYear)
set @FiscalDate = (select cast(datepart(mm,@InputDate) as varchar(2)) + '/' + cast(datepart(dd,@InputDate) as varchar(2)) + '/' + @FiscalYear)
set @FiscalMonth = (select
case
when datepart(mm,@InputDate) < cast(@FiscalInput as int)
then 13 + datediff(mm, cast(@FiscalStart as datetime),@InputDate)
when datepart(mm,@InputDate) >= cast(@FiscalInput as int)
then 1 + datediff(mm, cast(@FiscalStart as datetime), @FiscalDate)
end FiscalMonth)
select @InputDate as Date,
cast(@FiscalStart as datetime) as FiscalStart,
dateadd(mm, 11,cast(@FiscalStart as datetime)) as FiscalStop,
cast(@FiscalDate as DateTime) as FiscalDate,
@FiscalMonth as FiscalMonth,
@FiscalYear as FiscalYear
I just realized that the marked answer by Brett Veenstra is wrong. Shouldn't The FY should be calculated like this?:
CREATE FUNCTION dbo.fnc_FiscalYear(
@AsOf DATETIME
)
RETURNS INT
AS
BEGIN
DECLARE @Answer INT
IF ( MONTH(@AsOf) < 9 )
SET @Answer = YEAR(@AsOf)
ELSE
SET @Answer = YEAR(@AsOf) + 1
RETURN @Answer
END;
Here is the dynamic code for UK,
You can work around based on different needs,
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = DATEADD(dd, 0,
DATEDIFF(dd, 0,
DATEADD(mm, - (((12 + DATEPART(m, getDate())) - 4)%12), getDate()) -
datePart(d,DATEADD(mm, - (((12 + DATEPART(m, getDate())) - 4)%12),
getDate() )) + 1 ))
SET @EndDate = DATEADD(SS, -1, DATEADD(mm, 12, @StartDate))
SELECT @StartDate, @EndDate
DECLARE
@StartDate DATETIME,
@EndDate DATETIME
if month(getdate())>3
Begin
set @StartDate= convert(datetime, cast(year(getdate())-1 as varchar) + '-4-1')
set @EndDate= convert(datetime, cast(year(getdate()) as varchar) + '-3-31')
end
else
begin
set @StartDate= Convert(datetime, cast(year(getdate()) - 2 as varchar) + '-4-1')
set @EndDate= convert(datetime, cast(year(getdate())-1 as varchar) + '-3-31')
end
select @StartDate, @EndDate