what is the best way to concat fields that may contain null as if they were empty strings

From: Reid Thompson <reid(dot)thompson(at)ateb(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Reid Thompson <reid(dot)thompson(at)ateb(dot)com>
Subject: what is the best way to concat fields that may contain null as if they were empty strings
Date: 2009-10-09 16:06:30
Message-ID: 20091009160630.GB29644@ateb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Gould 2009-10-09 16:06:37 Re: interface for "non-SQL people"
Previous Message Gerhard Wiesinger 2009-10-09 15:57:49 Re: PostgreSQL reads each 8k block - no larger blocks are used - even on sequential scans