Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id ACBC2632FFB for ; Wed, 23 Dec 2009 04:44:47 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 71915-08 for ; Wed, 23 Dec 2009 08:44:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id C1FCA632B0C for ; Wed, 23 Dec 2009 04:44:39 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 9896C1E0443; Wed, 23 Dec 2009 16:44:34 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id TGXyFNGtn9db; Wed, 23 Dec 2009 16:44:32 +0800 (WST) Received: from [10.1.1.6] (124-169-133-89.dyn.iinet.net.au [124.169.133.89]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 534421E0440; Wed, 23 Dec 2009 16:44:32 +0800 (WST) Message-ID: <4B31D86D.9030000@postnewspapers.com.au> Date: Wed, 23 Dec 2009 16:44:29 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0 MIME-Version: 1.0 To: Bryce Nesbitt CC: sql pgsql Subject: Re: Using || operator to fold multiple columns into one References: <4B31D619.1090706@obviously.com> In-Reply-To: <4B31D619.1090706@obviously.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/70 X-Sequence-Number: 33508 On 23/12/2009 4:34 PM, Bryce Nesbitt wrote: > Dear experts, > > This point is confusing me with the || operator. I've got a table with > "one column per data type", like so: Dare I ask why? What problem are you trying to solve by doing this? > # select context_key,keyname,t_number||t_string||t_date||t_boolean as > value from context_keyvals; > > But it is not working, the columns always come up empty. Here's why: psql> SELECT ('TEST'||NULL) IS NULL; ?column? t -------- (1 row) `||' applied to anything and null returns null, since it is unknown what the "null" part of the expression. It makes sense if you think of null as "unknown" or "undefined". In your case, you should probably typecast each field to `text' and use `coalesce' to show the first non-null one. Alternately, you could use a CASE statement, eg: SELECT COALESCE( t_number::text, t:string::text, t_date::text, t_boolean::text) AS value; Also: You do have a CHECK constraint on the table that asserts that at most one of those entries may be non-null, right? If not, you're very likely to land up with entries with more than one t_something non-null sooner or later so I suggest adding one. -- Craig Ringer