Re: " " around fields with psql

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Steve Clark <sclark(at)netwolves(dot)com>
Cc: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>, pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: " " around fields with psql
Date: 2012-02-10 22:30:27
Message-ID: CAHyXU0wJ3d70zTP31Yr2POataMj21dev9S4qWDBo+paK1W7soA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Feb 10, 2012 at 1:33 PM, Steve Clark <sclark(at)netwolves(dot)com> wrote:
> On 02/10/2012 02:12 PM, Scott Marlowe wrote:
>
> On Fri, Feb 10, 2012 at 11:26 AM, Steve Clark <sclark(at)netwolves(dot)com> wrote:
>
> Hello,
>
> Is there a way with psql to get column output to be
> "data1","data2",...,"datan"
>
> I tried -F "," but that left off the first and last quote.
>
> I can't seem to find a way in the man page.
>
> Well, you can do it yourself kinda like this:
>
> select '""||field1||'", "||field2||'" from sometable where yada.
>
> Ok that will work

for 9.1+ you can use built in format() function for a lot of fields:
select format('"%s", "%s", "%s", "%s"', procpid, usename, waiting,
query_start) from pg_stat_activity;

also with recent postgres you can use hstore to convert virtually any
query as such:
select '"' || array_to_string(avals(hstore(a)), '", "') || '"' from
pg_stat_activity a;

postgres=# select '"' || array_to_string(avals(hstore(q)), '", "') ||
'"' from (select 1 as a,2 as b,3 as c) q;
?column?
---------------
"1", "2", "3"

etc.
merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Travers 2012-02-10 23:32:36 Re: Strategy for Primary Key Generation When Populating Table
Previous Message David Johnston 2012-02-10 20:21:40 Re: Strategy for Primary Key Generation When Populating Table