What's the most efficient way to calculate the first day of the current (Australian) Financial Year?
The Australian FY begins on 01-July.
E.g.
SELECT dbo.FinancialYearStart('30-Jun-2011')
returns 01-Jul-2010.
SELECT dbo.FinancialYearStart('01-Jul-2011')
returns 01-Jul-2011.
SELECT dbo.FinancialYearStart('02-Jul-2011')
returns 01-Jul-2011.
One DATEADD, one DATEDIFF, and a division:
SELECT DATEADD(year,DATEDIFF(month,'19010701','20110630')/12,'19010701')
Basically, you count the number of months since some arbitrary financial year's start date (I've picked 1901), divide that number by 12 (ignoring the remainder), and add that many years back to the same arbitrary year's start date.
I don't know if this is the most efficient, but it's fast at least...
create function dbo.FinancialYearStart
(
@CurrentDate datetime
)
returns datetime
as
begin
declare @CurrentYear int
,@FYDateThisYear datetime
,@FYDatePrevYear datetime
set @CurrentYear = datepart(year, @CurrentDate)
set @FYDateThisYear = '01-Jul-' + cast(@CurrentYear as varchar(4))
set @FYDatePrevYear = '01-Jul-' + cast(@CurrentYear-1 as varchar(4))
if @CurrentDate < @FYDateThisYear
begin
return @FYDatePrevYear
end
return @FYDateThisYear
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With