I’m a big noob.
Function B is part of the body of function A.
Inside a function A, I would like to create a variable to store the function B result in postgresql, and to use that result as a comparison argument just after, still inside function A.
I rode documentation and tried 2 approaches, but can’t get what I would like:
The variable I want to create is named remaining_time in the example below.
1st try:
CREATE OR REPLACE FUNCTION remaining_period(start DATE, months INT) RETURNS INTERVAL AS
$$
DECLARE
remaining_time = SELECT AGE((start + (months || ' months')::INTERVAL)::DATE, NOW()::DATE);
BEGIN
CASE WHEN remaining_time > INTERVAL '0 day' THEN remaining_time
ELSE INTERVAL '0 day'
END
$$
LANGUAGE sql IMMUTABLE;
2nd try:
CREATE OR REPLACE FUNCTION remaining_period(start DATE, months INT) RETURNS INTERVAL AS
$$
SELECT AGE((start + (months || ' months')::INTERVAL)::DATE, NOW()::DATE) AS remaining_time;
CASE WHEN remaining_time > INTERVAL '0 day' THEN remaining_time
ELSE INTERVAL '0 day'
$$
LANGUAGE sql IMMUTABLE;