CREATE OR REPLACE FUNCTION UDF_PMT (
InterestRate NUMERIC(18,8),
Nper INTEGER,
Pv NUMERIC(18,4),
Fv NUMERIC(18,4),
Typ INTEGER
)
RETURNS NUMERIC(18,2)
AS $$
SELECT round(
CASE
WHEN Typ = 0 THEN
(InterestRate / 100) /
(Power(1 + InterestRate / 100, Nper) - 1) *
(Pv * Power(1 + InterestRate / 100, Nper) + Fv)
WHEN Typ = 1 THEN
(InterestRate / 100) /
(Power(1 + InterestRate / 100, Nper) - 1) *
(Pv * Power(1 + InterestRate / 100, Nper) + Fv) /
(1 + InterestRate / 100)
END, 2)
$$ LANGUAGE SQL;