SQL Server 2012.
We have a business unit which uses non-standard quarterly reporting. The quarters start in November and end in October. To labour the point, Q1: Nov to Jan; Q2: Feb to Apr; Q3: May to Jul; Q4: Aug to Oct.
I'm trying to create a "simple and efficient" query that can take a given date and return the first and last dates of the previous "quarter" according to this non-standard scheme.
A former colleague had dealt with this using a convoluted case expression which is pretty unwieldy, and I would like to find a better way.
I know SQL server has some built-in date logic that deals with quarters, but only the standard calendar quarters. So, the following will reliably return the first day of the previous quarter, but just the standard scheme of quarters beginning in January and ending in December...
SELECT DATEADD(quarter, DATEDIFF(quarter, 0, GETDATE()) - 1, 0)
--Shows first day of prior quarter, but only for 'standard' quarters
I then naively thought just subtracting two months would do it, but this is wrong 2/3 of the time
SELECT DATEADD(month, -2, DATEADD(quarter, DATEDIFF(quarter, 0, GETDATE()) - 1, 0))
--Works for 1/3 dates
Since then I've tried various nested calculation schemes and come up with nothing that works.
Can anyone help me with this calculation - or alternately would you recommend I just create a date table that lets me look up the non-standard quarter? Thanks