what is the best way to concat fields that may contain null as if they were empty strings - Mailing list pgsql-general

From Reid Thompson
Subject what is the best way to concat fields that may contain null as if they were empty strings
Date
Msg-id 20091009160630.GB29644@ateb.com
Whole thread Raw
Responses Re: what is the best way to concat fields that may contain null as if they were empty strings
List pgsql-general
In the case where a_text is null, I essentially want the same result as the case when a_text = ''.

would this:
  select a_int || coalesce(a_text,'') from test1 where a_int = 1000002;
be the proper way?


postgres=# \d test1
                                     Table "public.test1"
 Column |            Type             |                       Modifiers
--------+-----------------------------+-------------------------------------------------------
 a_int  | integer                     | not null default nextval('test1_a_int_seq'::regclass)
 a_text | character varying(200)      |
 dt     | timestamp without time zone | default now()
Indexes:
    "test1_pkey" PRIMARY KEY, btree (a_int)

postgres=# select count(*) from test1;
  count
---------
 1000000
(1 row)

postgres=# insert into test1(a_text) values('');
INSERT 0 1
postgres=# select max(a_int) from test1;
   max
---------
 1000001
(1 row)

postgres=# select a_int || a_text from test1 where a_int = 1000001;
 ?column?
----------
 1000001
(1 row)

postgres=# insert into test1(a_text) values(null);
INSERT 0 1
postgres=# select a_int || a_text from test1 where a_int = 1000002;
 ?column?
----------

(1 row)

postgres=# select * from test1 where a_int >= 1000001;
  a_int  | a_text |             dt
---------+--------+----------------------------
 1000001 |        | 2009-10-09 11:54:38.455556
 1000002 |        | 2009-10-09 11:56:00.37607
(2 rows)





pgsql-general by date:

Previous
From: Andrew Gould
Date:
Subject: Re: interface for "non-SQL people"
Next
From: Whit Armstrong
Date:
Subject: array question