1

I'm trying to create user-defined function which will return SUM from expenses view based on given dates.

CREATE FUNCTION budget.getTotalAmountFromView (startDate DATE, endDate DATE)
RETURNS DECIMAL AS $$
DECLARE
    totalValue DECIMAL := 0;
BEGIN
    SELECT INTO totalValue sum(amount) from budget.epenses_overview where transaction_date >= startDate AND transaction_date <= endDate;
    RETURN totalValue;
END;
$$ LANGUAGE plpgsql;

I am trying to invoke it using:

SELECT * FROM budget.getTotalAmountFromView(TO_DATE(20190201, YYYYMMDD), TO_DATE(20190225, YYYYMMDD));

But it returns error

AFTER CHANGES:
Function shall be assigned to the right schema -> budget;
and invoke:
SELECT budget.getTotalAmountFromView('20190201'::DATE, '20190225'::DATE);

1
  • 2
    What error does it return? Commented Feb 12, 2020 at 20:51

2 Answers 2

1

You don't need FROM for scalar function:

SELECT budget.getTotalAmountFromView22('20190201'::DATE, '20190225'::DATE);
Sign up to request clarification or add additional context in comments.

Comments

0

You are lacking single quotes around the date and format strings.

TO_DATE(20190201, YYYYMMDD) should be TO_DATE('20190201', 'YYYYMMDD')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.