Re: string concatenation - Mailing list pgsql-general

From Bruce Momjian
Subject Re: string concatenation
Date
Msg-id 200210141725.g9EHPsU27624@candle.pha.pa.us
Whole thread Raw
In response to string concatenation  ("Gyorgy Molnar" <yuri@powercom.com.sg>)
List 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@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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: drop constraint unnamed?
Next
From: Tom Lane
Date:
Subject: Re: string concatenation