Most efficient way to calculate the first day of the current Financial Year?

Extract the year and month from the date. Then do year = year + FLOOR((month-7) / 6)

Then your date is 1-jul-year

(You don't actually need to store them as variables.)

Something like: CONCATENATE('01-jul-', YEAR(date) + FLOOR((MONTH(date)-7) / 6)


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