Re: Is there a conditional string-concatenation ?

Поиск
Список
Период
Сортировка
От Osvaldo Kussama
Тема Re: Is there a conditional string-concatenation ?
Дата
Msg-id AANLkTikZZPYGOVB_JJn5ZEFfEb7J=BQiLLJJRW9LL81c@mail.gmail.com
обсуждение исходный текст
Ответ на Is there a conditional string-concatenation ?  (Andreas <maps.on@gmx.net>)
Ответы Re: Is there a conditional string-concatenation ?  (Pavel Stehule <pavel.stehule@gmail.com>)
Список 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_DoeJohnDoe
JohnJohnDoe
(6 rows)

Osvaldo


В списке pgsql-sql по дате отправления:

Предыдущее
От: Joshua Tolley
Дата:
Сообщение: Re: Is there a conditional string-concatenation ?
Следующее
От: Viktor Bojović
Дата:
Сообщение: Re: get attribute from XML