Re: NULL concatenation

From: George Neuner <gneuner2(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: NULL concatenation
Date: 2016-05-13 06:49:36
Message-ID: bjtajb9dhgl9arm6pe1v9csi6s97el53li@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Fri, 13 May 2016 08:45:46 +0530, Sridhar N Bamandlapally
<sridhar(dot)bn1(at)gmail(dot)com> wrote:

>we need simple concatenation of all variables(which values may come NULL or
>valid-values based on functional process),
>
>coalesce is different functionality

As Pavel suggested, concat will work, but it swallows NULLs leaving no
trace of them in the output. Using coalesce *with* concat lets you
decide what a NULL will look like:

e.g.,

>> do $$
>> declare
>> txt1 text := 'ABCD';
>> txt2 text := NULL;
>> txt3 text := 'EFGH';
>> txt text := NULL;
>> begin

txt := coalesce( txt1, '' )
|| coalesce( txt2, 'txt2 was null' )
|| coalesce( txt3, '<null>') ;

>> raise notice '%', txt;
>> end$$ language plpgsql;

George

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Arthur Silva 2016-05-13 07:06:52 Re: Share my experience and Thank you !
Previous Message JingYuan Chen 2016-05-13 06:44:31 Share my experience and Thank you !

Browse pgsql-hackers by date

  From Date Subject
Next Message Etsuro Fujita 2016-05-13 07:00:31 Re: Use %u to print user mapping's umid and userid
Previous Message Michael Paquier 2016-05-13 06:39:35 pg_basebackup, pg_receivexlog and data durability (was: silent data loss with ext4 / all current versions)