Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8879F632FFB for ; Wed, 23 Dec 2009 04:50:45 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 26773-09 for ; Wed, 23 Dec 2009 08:50:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 9E03A6323B6 for ; Wed, 23 Dec 2009 04:50:34 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0MZyo7-1NiTDE3P03-00LtyR; Wed, 23 Dec 2009 09:50:32 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1NNMvP-0003BY-00 for pgsql-sql@postgresql.org; Wed, 23 Dec 2009 09:50:31 +0100 Date: Wed, 23 Dec 2009 09:50:31 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Using || operator to fold multiple columns into one Message-ID: <20091223085031.GA12007@a-kretschmer.de> References: <4B31D619.1090706@obviously.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4B31D619.1090706@obviously.com> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18meeMvqnaBxGvLI0RdKv9iC+zFu4RsL+K2Py/ TF2O1NKC0smrLBWdr4XArzMG4AiGuw5swKIAh8i8+DZ11WuWw8 WKLSrND7DJmbzX5QUbtlg== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.254 tagged_above=-10 required=5 tests=AWL=0.345, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/71 X-Sequence-Number: 33509 In response to Bryce Nesbitt : > Dear experts, > > This point is confusing me with the || operator. I've got a table with > "one column per data type", like so: > > # \d context_keyvals; > Table "public.context_keyvals" > Column | Type | Modifiers > -------------+-----------------------------+----------- > context_key | integer | not null > keyname | text | > t_number | integer | > t_string | text | > t_boolean | boolean | > t_date | timestamp without time zone | > Indexes: > "context_keyvals_ck" btree (context_key) CLUSTER > Foreign-key constraints: > "context_keyvals_context_key_fkey" FOREIGN KEY (context_key) > REFERENCES contexts(context_key) ON DELETE CASCADE > # select version() > PostgreSQL 8.3.8 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real > (Debian 4.3.2-1.1) 4.3.2 > > ------------------------------ > Just for pretty sake I'd like to be able to use psql to view it like 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. I can use the > || operator to concatenate strings: > # select '--'||t_number::text from context_keyvals; > > But the moment I try to combine columns, the result is blank. > # select '--'||t_number::text||t_string::text from context_keyvals; At least one column contains NULL. To handle that, use coalesce(column,'') to change NULL to ''. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99