I have, as part of a larger postgres query, a query whose purpose is to generate each year that exists between two given dates:
select to_char(generate_series, 'YYYY') from generate_series(
'2022-06-14 11:00:00.000+00'::timestamptz,
'2023-06-14 11:00:00.000+00'::timestamptz,
'1 year' );
Which returns:
"2022"
"2023"
The issue is if the date range given is less than one year, the years returned is only the first. For my purposes I require it to be “inclusive” if the date range spans multiple years, but is less than one year, e.g.
select to_char(generate_series, 'YYYY') from generate_series(
'2022-06-14 11:00:00.000+00'::timestamptz,
'2023-06-13 11:00:00.000+00'::timestamptz,
'1 year' );
Only returns:
"2022"
But I would like it to return:
"2022"
"2023"
Is there some way to achieve this?
Thanks!