I’m trying to create a table-valued function in SQL that takes two date parameters and returns a monthly breakdown with quarters, semi-annual, and yearly data.
What I want to achieve:
Call the function like: select * from getDatePeriods('2012-01-01','2015-09-09')
Expected output format:
month_num quarter half_year year_val
1 1 1 2012
2 1 1 2012
3 1 1 2012
4 2 1 2012
5 2 1 2012
6 2 1 2012
7 3 2 2012
8 3 2 2012
9 3 2 2012
... ... ... ...
9 3 2 2015
My current attempt:
create function getDatePeriods
(@beginDate date, @finishDate date)
RETURNS @result table
(month_num int, quarter int, half_year int, year_val int)
AS
BEGIN
declare
@month_num int,
@quarter int,
@half_year int,
@year_val int;
select @month_num = DATEDIFF(MONTH, @beginDate, @finishDate);
select @quarter = DATEDIFF(QUARTER, @beginDate, @finishDate);
select @half_year = DATEDIFF(QUARTER, @beginDate, @finishDate) / 2;
select @year_val = DATEDIFF(YEAR, @beginDate, @finishDate);
WHILE (@finishDate > @beginDate)
BEGIN
insert into @result
select @month_num, @quarter, @half_year, @year_val;
END;
return;
END;
The function isn’t working as expected. How can I fix this to generate the correct monthly rows with proper quarter and semi-annual calculations for each month in the date range?