Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing what quarter of a financial year a date is in

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.

like image 774
Michael A Avatar asked Jan 05 '12 07:01

Michael A


People also ask

What are quarter Dates for 2022?

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.

How do I get a financial quarter from a date in Excel?

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.

What is the Q4 of 2022?

When is quarter-end? The current quarter Q4, ends on 12/31/2022 in 55 days (40 business days).

What is a quarter in financial year?

A financial year is usually divided into 4 equal quarters. Quarter Q1 - April - June Q2 - July - September Q3 - October - December Q4 - January - MarchPBT margin (


2 Answers

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"
like image 152
Adam Ralph Avatar answered Nov 01 '22 20:11

Adam Ralph


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;
like image 29
Granger Avatar answered Nov 01 '22 22:11

Granger