pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Is there a conditional string-concatenation ?
4+ messages / 4 participants
[nested] [flat]

* Is there a conditional string-concatenation ?
@ 2010-10-12 04:09  Andreas <maps.on@gmx.net>
  0 siblings, 2 replies; 4+ messages in thread

From: Andreas @ 2010-10-12 04:09 UTC (permalink / raw)
  To: pgsql-sql

  Hi,
Is there a conditional string-concatenation ?

I'd like to have an elegant way to connect 2 strings with some 3rd 
element between only if there really are 2 strings to connect.

e.g.
MyCat ( 'John', '_', 'Doe' ) --> 'John_Doe'
while
MyCat ( 'John', '_', '' ) --> 'John'
MyCat ( '', '_', 'Doe' ) --> 'Doe'
MyCat ( '', '_', '' ) --> NULL

It should treat  NULL  and  ''  equally as empty
and it should trim each of the 3 elements.

so
MyCat ( '       John     ', '_', NULL ) --> 'John'
MyCat ( 'John', NULL, 'Doe' ) --> 'JohnDoe'





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Is there a conditional string-concatenation ?
@ 2010-10-12 04:18  Joshua Tolley <eggyknap@gmail.com>
  parent: Andreas <maps.on@gmx.net>
  1 sibling, 0 replies; 4+ messages in thread

From: Joshua Tolley @ 2010-10-12 04:18 UTC (permalink / raw)
  To: Andreas <maps.on@gmx.net>; +Cc: pgsql-sql

On Tue, Oct 12, 2010 at 06:09:07AM +0200, Andreas wrote:
>  Hi,
> Is there a conditional string-concatenation ?

Perhaps this:

CREATE OR REPLACE FUNCTION mycat(text, text, text) RETURNS TEXT LANGUAGE sql
IMMUTABLE AS $$
SELECT CASE
    WHEN $1 IS NULL OR $1 = '' THEN trim($3)
    WHEN $3 IS NULL OR $3 = '' THEN trim($1)
    ELSE trim($1) || trim(coalesce($2, '')) || trim($3)
END;

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachments:

  [application/pgp-signature] signature.asc (196B, ../../4cb3e194.ea05ec0a.3e4b.06b2@mx.google.com/2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Is there a conditional string-concatenation ?
@ 2010-10-12 05:03  Osvaldo Kussama <osvaldo.kussama@gmail.com>
  parent: Andreas <maps.on@gmx.net>
  1 sibling, 1 reply; 4+ messages in thread

From: Osvaldo Kussama @ 2010-10-12 05:03 UTC (permalink / raw)
  To: Andreas <maps.on@gmx.net>; +Cc: pgsql-sql

2010/10/12 Andreas <maps.on@gmx.net>:
>  Hi,
> Is there a conditional string-concatenation ?
>
> I'd like to have an elegant way to connect 2 strings with some 3rd element
> between only if there really are 2 strings to connect.
>
> e.g.
> MyCat ( 'John', '_', 'Doe' ) --> 'John_Doe'
> while
> MyCat ( 'John', '_', '' ) --> 'John'
> MyCat ( '', '_', 'Doe' ) --> 'Doe'
> MyCat ( '', '_', '' ) --> NULL
>
> It should treat  NULL  and  ''  equally as empty
> and it should trim each of the 3 elements.
>
> so
> MyCat ( '       John     ', '_', NULL ) --> 'John'
> MyCat ( 'John', NULL, 'Doe' ) --> 'JohnDoe'
>


Try:
bdteste=# SELECT nullif(ltrim(rtrim(coalesce(c1,'') || coalesce(c2,'')
|| coalesce(c3,''),' _'),' _'),'')
bdteste-#   FROM (VALUES ('John', '_', 'Doe'),('John', '_', ''),('',
'_', 'Doe'),('', '_', ''),('       John     ', '_', NULL),('John',
NULL, 'Doe')) AS foo(c1,c2,c3);
  nullif
----------
 John_Doe
 John
 Doe

 John
 JohnDoe
(6 rows)

Osvaldo



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Is there a conditional string-concatenation ?
@ 2010-10-12 09:01  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Osvaldo Kussama <osvaldo.kussama@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2010-10-12 09:01 UTC (permalink / raw)
  To: Osvaldo Kussama <osvaldo.kussama@gmail.com>; +Cc: Andreas <maps.on@gmx.net>; pgsql-sql

Hello

more simply

postgres=# CREATE OR REPLACE FUNCTION public.foo(a text, b text, c text)
 RETURNS text
 LANGUAGE sql
AS $function$
SELECT coalesce($1 || $2 || $3,
              $1 || $2,
              $2 || $3)
$function$

Regards

Pavel Stehule

2010/10/12 Osvaldo Kussama <osvaldo.kussama@gmail.com>:
> 2010/10/12 Andreas <maps.on@gmx.net>:
>>  Hi,
>> Is there a conditional string-concatenation ?
>>
>> I'd like to have an elegant way to connect 2 strings with some 3rd element
>> between only if there really are 2 strings to connect.
>>
>> e.g.
>> MyCat ( 'John', '_', 'Doe' ) --> 'John_Doe'
>> while
>> MyCat ( 'John', '_', '' ) --> 'John'
>> MyCat ( '', '_', 'Doe' ) --> 'Doe'
>> MyCat ( '', '_', '' ) --> NULL
>>
>> It should treat  NULL  and  ''  equally as empty
>> and it should trim each of the 3 elements.
>>
>> so
>> MyCat ( '       John     ', '_', NULL ) --> 'John'
>> MyCat ( 'John', NULL, 'Doe' ) --> 'JohnDoe'
>>
>
>
> Try:
> bdteste=# SELECT nullif(ltrim(rtrim(coalesce(c1,'') || coalesce(c2,'')
> || coalesce(c3,''),' _'),' _'),'')
> bdteste-#   FROM (VALUES ('John', '_', 'Doe'),('John', '_', ''),('',
> '_', 'Doe'),('', '_', ''),('       John     ', '_', NULL),('John',
> NULL, 'Doe')) AS foo(c1,c2,c3);
>  nullif
> ----------
>  John_Doe
>  John
>  Doe
>
>  John
>  JohnDoe
> (6 rows)
>
> Osvaldo
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2010-10-12 09:01 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-10-12 04:09 Is there a conditional string-concatenation ? Andreas <maps.on@gmx.net>
2010-10-12 04:18 ` Joshua Tolley <eggyknap@gmail.com>
2010-10-12 05:03 ` Osvaldo Kussama <osvaldo.kussama@gmail.com>
2010-10-12 09:01   ` Pavel Stehule <pavel.stehule@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox