I have a requirement of extracting data of given quarter.
For example if I pass
declare @year int=2021
declare @q int=2
the function should return
year quarterfrom quarterto
--------------------------------
2021 2021-04-01 2021-06-30
-----------------------------------
How can we achieve that in MSSQL (2012)
CodePudding user response:
Some fairly trivial date maths works:
DECLARE @year int = 2021,
@q int = 2;
SELECT @Year AS Year,
DATEADD(MONTH, 3 * (@q-1),DATEFROMPARTS(@year,1,1)),
EOMONTH(DATEADD(MONTH, (3 * (@q-1)) 2,DATEFROMPARTS(@year,1,1)));
This, of course, assumes that the quarters start on the 1st, 4th, 7th and 10th months.