I'm trying to construct a query that will map two columns, one, a date from a table, the second column an alias to show what quarter and financial year the date falls into.
Unfortunately I don't have enough knowledge of SQL to know where to begin. I know that I'd do this with a combination of getdate()
and dateadd(MONTH,,)
however nothing that I've put together has come close to working.
To further complicate this the financial years in Australia go from 1st July- 30th of June so quarter one of financial year 2012 would start from July 1st 2012.
I can do this without a statement however I'd much rather have it in a statement as it's going to be used in an SSRS report and a C# application and it would make maintenance significantly easier.
Dates for Q1: January 1 – March 31. Dates for Q2: April 1 – June 3. Dates for Q3: July 1 – September 30. Dates for Q4: October 1 – December 31.
Convert date to fiscal quarter Then in a cell next to your date column, and type this formula =CHOOSE(MONTH(A6),3,3,3,4,4,4,1,1,1,2,2,2) into it, then drag the fill handle to a range you need. Tip: In the above formula, A6 is the date cell, and the number series 3,3,3…is the fiscal quarter series you typed in step 1.
When is quarter-end? The current quarter Q4, ends on 12/31/2022 in 55 days (40 business days).
A financial year is usually divided into 4 equal quarters. Quarter Q1 - April - June Q2 - July - September Q3 - October - December Q4 - January - MarchPBT margin (
This should work:-
SELECT
MyDate,
CASE
WHEN MONTH(MyDate) BETWEEN 1 AND 3 THEN convert(char(4), YEAR(MyDate) - 1) + 'Q3'
WHEN MONTH(MyDate) BETWEEN 4 AND 6 THEN convert(char(4), YEAR(MyDate) - 1) + 'Q4'
WHEN MONTH(MyDate) BETWEEN 7 AND 9 THEN convert(char(4), YEAR(MyDate) - 0) + 'Q1'
WHEN MONTH(MyDate) BETWEEN 10 AND 12 THEN convert(char(4), YEAR(MyDate) - 0) + 'Q2'
END AS Quarter
FROM
MyTable
Output:-
MyDate Quarter
---------- --------
2011-01-01 "2010Q3"
2011-04-01 "2010Q4"
2011-07-01 "2011Q1"
2011-10-01 "2011Q2"
This method doesn't require a CASE statement, and works for any monthly fiscal calendar.
DECLARE @firstMonthOfFiscalQ1 int = 7; --1=January
SELECT DateColumn
, FLOOR(((12 + MONTH(DateColumn) - @firstMonthOfFiscalQ1) % 12) / 3 ) + 1 AS FiscalQuarter
FROM SomeTable
WHERE 1=1;
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