From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | Osvaldo Kussama <osvaldo(dot)kussama(at)gmail(dot)com> |
Cc: | Andreas <maps(dot)on(at)gmx(dot)net>, pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Is there a conditional string-concatenation ? |
Date: | 2010-10-12 09:01:20 |
Message-ID: | AANLkTi=5TVHZ46DpBdW5=fZZJE8PZr6NDiyJYbNsGEnu@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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(dot)kussama(at)gmail(dot)com>:
> 2010/10/12 Andreas <maps(dot)on(at)gmx(dot)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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
From | Date | Subject | |
---|---|---|---|
Next Message | Gary Chambers | 2010-10-12 14:19:24 | Re: Duplicates Processing |
Previous Message | Viktor Bojović | 2010-10-12 07:10:06 | Re: get attribute from XML |