Re: Concat error in PL/pgsql

Lists: pgsql-bugs
From: Hans Peter Wuermli <wurmli(at)freesurf(dot)ch>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Concat error in PL/pgsql
Date: 2001-01-06 17:29:22
Message-ID: 200101061729.SAA31739@obelix.spectraweb.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


I don't have a bug template at hand.

Within a plpgsql function, concatenating TEXT strings from tables that allow NULL values return nil results. Please run the following. The result of the second select is nil.

Cheers, H.P.

-----------------------------------------------------------

create table tconcattest (id char(3),str text);

insert into tconcattest values('hpw',text('Something...'));
insert into tconcattest values('wuh',text('and more of something.'));

create function fconcattest () returns text as'
declare
r record;
output text;
begin
output := text('''');
for r in select * from tconcattest loop
output := output || r.str;
end loop;
return output;
end;
' language 'plpgsql';

select fconcattest();

insert into tconcattest values('abc',NULL);

select fconcattest();

update tconcattest set str='...again' where id='abc';

select fconcattest();


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Hans Peter Wuermli <wurmli(at)freesurf(dot)ch>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Concat error in PL/pgsql
Date: 2001-01-06 21:38:21
Message-ID: 18636.978817101@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Hans Peter Wuermli <wurmli(at)freesurf(dot)ch> writes:
> Within a plpgsql function, concatenating TEXT strings from tables that
> allow NULL values return nil results.

That's not a bug: NULL concatenated with anything produces NULL,
per SQL92 specification.

If you want a NULL to act like an empty string, try

for r in select * from tconcattest loop
output := output || coalesce(r.str, '''');

regards, tom lane


From: "Andrew Snow" <andrew(at)modulus(dot)org>
To: "Hans Peter Wuermli" <wurmli(at)freesurf(dot)ch>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: RE: Concat error in PL/pgsql
Date: 2001-01-07 00:57:55
Message-ID: JEEGIJPOJIGGIIHGKOKOEELLCHAA.andrew@modulus.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


Simple add lines as shown:

> for r in select * from tconcattest loop
IF r.str IS NOT NULL THEN
> output := output || r.str;
END IF;
> end loop;

- Andrew


From: "Andrew Snow" <andrew(at)modulus(dot)org>
To: <pgsql-bugs(at)postgresql(dot)org>
Subject: RE: Concat error in PL/pgsql
Date: 2001-01-07 08:51:51
Message-ID: JEEGIJPOJIGGIIHGKOKOCEMACHAA.andrew@modulus.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


A few hours ago, I wrote:
>
> Simple add lines as shown:
>
> > for r in select * from tconcattest loop
> IF r.str IS NOT NULL THEN
> > output := output || r.str;
> END IF;
> > end loop;
>

This would probably be better:

for r in select * from tconcattest where str is not null loop
output := output || r.str;
end loop;