@RogierPronk gave you 2 options - let's take a look at each one:
DATEFROMPARTS({year}, {month}, {day})
For today - 2024-03-12 - using this DATEFROMPARTS(year(getdate()), 7, 1) returns 2024-07-01 which would be the start of fiscal year 2025 (not correct). Instead - we want DATEFROMPARTS(year(getdate()) - 1, 7, 1) which would return 2023-07-01 (start of fiscal year 2024).
For the end of the range we want DATEFROMPARTS({year}, 6, 30) - and this DATEFROMPARTS(year(getdate()), 6, 3) returns 2024-06-30.
However, if the date is 2023-11-15 - we get 2023-06-30 which is not correct.
So - we need to calculate for either calculation when to add or subtract 1 from the year. To do that, we just need to check the month:
SELECT DATEFROMPARTS(year(getdate()) - iif(month(getdate()) < 7, 1, 0), 7, 1);
SELECT DATEFROMPARTS(year(getdate()) + iif(month(getdate()) >= 7, 1, 0), 6, 30);
If the date is 2023-11-15 - we get DATEFROMPARTS(2023 - 0, 7, 1) = 2023-07-01 and DATEFROMPARTS(2023 + 1, 6, 30) = 2024-06-30.
If the date is 2024-03-12 we get DATEFROMPARTS(2024 - 1, 7, 1) = 2023-07-01 and DATEFROMPARTS(2024 + 0, 6, 30) = 2024-06-30.
The other solution is to lookup the start/end dates of the fiscal year based on the date. Given a table of fiscal years - where we have the start and end dates of the fiscal year - we can lookup the start and end dates.
SELECT TOP (1)
*
FROM fiscal_calendar
WHERE StartDate <= getdate()
ORDER BY
StartDate DESC;
The above gets the latest row from the fiscal calender that has a start date less than or equal to today. So now all we need to do is return the actual start and end dates in variables:
DECLARE @StartDate date
, @EndDate date;
SELECT TOP (1)
@StartDate = StartDate
, @EndDate = EndDate
FROM fiscal_calendar
WHERE StartDate <= getdate()
ORDER BY
StartDate DESC;
SELECT a.company
, a.code
, a.contract
, a.amount
FROM Allocated_amounts as a
WHERE a.start_date >= @StartDate
AND a.end_date <= @EndDate;
One caveat - if your date columns in the table are actually datetime then you need to account for the times. If that is the case, then you would add one day to the end date and change the check for that end date to less than: AND a.end_date < DATEADD(DAY, 1, @EndDate)