0

I am new to PostgresQL and having some trouble in feeding a function with a variable from an outer function. (maybe it is not possible...)

Inside a function I am trying to assign a value from calling another function like this: left_bipolar_n:=public.convert_to_decimal(left_bipolar);

When I call the function, If I use something like '122' as an argument instead of "left_bipolar" it works just fine but just as it is I get: Error: invalid input syntax for type numeric: " "

I should say that Function convert_to_decimal expects text

Complete code:

-- FUNCTION: public.AmateurExercise(numeric)
--drop FUNCTION public."AmateurExercise"(numeric);

CREATE OR REPLACE FUNCTION public."AmateurExercise"(
    n numeric)
    RETURNS numeric
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
declare 
bipolar text;
tamanho integer;
left_bipolar text;
right_bipolar text;
left_bipolar_n numeric;
right_bipolar_n numeric;

begin
bipolar:=public.convert_to_bipolar8(n);
tamanho=length(bipolar);
if tamanho/2 =floor (tamanho/2) then 
begin
left_bipolar=left(bipolar,tamanho/2);
right_bipolar=right(bipolar, tamanho/2);
end;
else
left_bipolar=left(bipolar,(tamanho-1)/2);
right_bipolar=right(bipolar, ((tamanho-1)/2)+1);
end if;

left_bipolar_n:=public.convert_to_decimal(left_bipolar);
return left_bipolar_n;

end;
$BODY$;

ALTER FUNCTION public."AmateurExercise"(numeric)
    OWNER TO postgres;
3
  • The error indicates that for some reason left_bipolar contains a space just before the call. I guess convert_to_bipolar8 must put the space there. But we can't see the code for it, not what input you called the outer function with.
    – jjanes
    Commented Jan 30, 2021 at 0:10
  • @jjanes: right on target. Once I remove the space it works just fine. Thanks a lot! Commented Jan 30, 2021 at 1:23
  • Please put this in as an answer and mark it as correct!
    – Vérace
    Commented Jan 30, 2021 at 12:10

1 Answer 1

0

answer from a jjanes comment:

"The error indicates that for some reason left_bipolar contains a space just before the call. I guess convert_to_bipolar8 must put the space there. But we can't see the code for it, not what input you called the outer function with"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.