Re: string concatenation

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Gyorgy Molnar <yuri(at)powercom(dot)com(dot)sg>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: string concatenation
Date: 2002-10-14 17:25:54
Message-ID: 200210141725.g9EHPsU27624@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-10-14 17:31:47 Re: string concatenation
Previous Message Tom Lane 2002-10-14 17:24:31 Re: drop constraint unnamed?