1

Here I am trying to assign value to variable which is null by default. For example as shown below in the example.

Example:

create or replace function fun(va varchar)
returns void as
$$
declare
       var1 varchar := null; 
begin
       var1 := var1 || va;

       raise notice '%',var1;
end;
$$
language plpgsql;

Calling Function:

select fun('abc');

Output:

NOTICE:  <NULL>

Note: I am unable to get output as abc?

1 Answer 1

4

Any concatenation with null yields null, so 'abc'||null is null.

If you want abc as the output you have to initialize var1 to an empty string:

var1 varchar := ''; 

Another possible solution is to use concat() which ignores null values, so concat('abc',null') returns 'abc'

Sign up to request clarification or add additional context in comments.

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.