Re: string concatenation

Started by Bruce Momjianover 23 years ago1 messagesgeneral
Jump to latest
#1Bruce Momjian
bruce@momjian.us

Gyorgy Molnar wrote:

plase take a look to the following small sample

CREATE FUNCTION test(TEXT) RETURNS TEXT AS '
DECLARE
cmd TEXT;
BEGIN
RETURN cmd || ''Hello'';
END;
' LANGUAGE 'plpgsql';

When I execute the function

select test('');
test
------

(1 row)

I got an empty string for result. I think I got this result because the
string concatenation ("||") was created with "isstrict" flag. In this case
it will give back NULL object if one of the arguments was NULL object.

In most of the cases I think it is ok, but specially for the string
concatenation is not really convinient.

Is this feature will change in the future? Any "smart" solution for this
problem? - I cannot use the IF..ENDIF statement before each string
concatenation, I have too many in my code.

The function in incorrect. cmd is NULL because you haven't used $1.
Try:

test=> CREATE FUNCTION test2(TEXT) RETURNS TEXT AS '
test'> BEGIN
test'> RETURN $1 || ''Hello'';
test'> END;
test'> ' LANGUAGE 'plpgsql';
CREATE FUNCTION
test=> select test2('a');
test2
--------
aHello
(1 row)

test=> select test2('');
test2
-------
Hello
(1 row)

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073