I'm trying to create a function in sql but its not working, which accepts a parameter of @year int
returns a data type of "money"
use the @year to return the amount from Ref_table where the @year falls between begin_year and end_year. if there is no value, return the amount from
Ref_table with the highest end_year that is less than @year*/
here is my code
CREATE FUNCTION fn_year(
@year int
)
RETURNS money
as
begin
declare @begin_year int
declare @end_year int
declare @amount money
select @begin_year = begin_year,
@end_year = end_year
from DBO.REF_Table
if @year between year(@begin_year) and year(@end_year)
select @amount = amount
from dbo.REF_Table
end
else if @amount is null
begin
select @amount = amount
from dbo.REF_Table
where max @end_year
and @end_year < @year
end
End
GO