As I have to calculate Start date of specific quarter and Quarter No from any financial year start date based on @firstMonthOfFiscalyear parameter. Let's say if @firstMonthOfFiscalyear =4 mean my financial year start date is 1 April and my quarter no start as below. Q1 - April to Jun Q2 - July to Sep Q3 - Oct to Dec Q4 - Jan to March
This quarter no will change based on @firstMonthOfFiscalyear parameter value.
From this I am able to get Quarter number but not able to get Start date of that quarter. So anyone can help me on this.
DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME
DECLARE @firstMonthOfFiscalyear int = 4 --Finanical year start month
SET @StartDateTime = '2017-04-01'
SET @EndDateTime = '2019-03-31';
WITH DateRange(Dates) AS
(
SELECT @StartDateTime as Date
Union ALL
SELECT DATEADD(d,1,Dates)
FROM DateRange
WHERE Dates < @EndDateTime
)
SELECT Dates
,FLOOR(((12 + MONTH(Dates) - @firstMonthOfFiscalyear) % 12) / 3 ) + 1 as quarterNo
, DATEADD(month, (IIF((month(dates)-@firstMonthOfFiscalyear)<0,(month(dates)-@firstMonthOfFiscalyear)+12,(month(dates)-@firstMonthOfFiscalyear))/3)*3, CAST( DATEFROMPARTS(year(dates),@firstMonthOfFiscalyear ,1) as Datetime)) as QuarterStartDate
FROM DateRange
OPTION (MAXRECURSION 0)