Re: Conactenating text with null values

From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
Cc: olly(at)lfix(dot)co(dot)uk, "Gregory S(dot) Williamson" <gsw(at)globexplorer(dot)com>, Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Conactenating text with null values
Date: 2004-11-05 17:16:34
Message-ID: 1099674994.6361.86.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Cool, this goes to my "util" mail folder :-)

[snip]
> But it's still too cumbersome. How about creating a new operator? With
> the example below the query would simply be
>
> SELECT s_host ||~ s_post_dir ||~ s_street ||~ s_suffix
> FROM parcels
> WHERE s_pin = '1201703303520';
>
> alvherre=# CREATE FUNCTION text_concat_nulls_with_an_embedded_space(text, text) RETURNS text AS 'SELECT CASE WHEN $1 IS NULL THEN $2 WHEN $2 IS NULL THEN $1 ELSE $1 || '' '' || $2 END' LANGUAGE sql;
> CREATE FUNCTION
> alvherre=# SELECT text_concat_nulls_with_an_embedded_space('foo', NULL);
> text_concat_nulls_with_an_embedded_space
> ------------------------------------------
> foo
> (1 fila)
>
> alvherre=# SELECT text_concat_nulls_with_an_embedded_space(NULL, 'foo');
> text_concat_nulls_with_an_embedded_space
> ------------------------------------------
> foo
> (1 fila)
>
> alvherre=# SELECT text_concat_nulls_with_an_embedded_space('bar', 'foo');
> text_concat_nulls_with_an_embedded_space
> ------------------------------------------
> bar foo
> (1 fila)
> alvherre=# CREATE OPERATOR ||~ (PROCEDURE = text_concat_nulls_with_an_embedded_space, LEFTARG = text, RIGHTARG = text);
> CREATE OPERATOR
> alvherre=# SELECT 'hi' ||~ 'foo' ||~ null ||~ null ||~ 'bar' ||~ 'baz' ||~ null ||~ 'bye';
> ?column?
> --------------------
> hi foo bar baz bye
> (1 fila)
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mario A. Soto Cordones 2004-11-05 17:18:03 Trigger Parameter problem
Previous Message Andrew Lazarus 2004-11-05 16:50:23 Using EXECUTE in same manner as SELECT