Re: Query output into a space delimited/location sensitive file

From: Greg Smith <gsmith(at)gregsmith(dot)com>
To: Ubence Quevedo <r0d3nt(at)pacbell(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Query output into a space delimited/location sensitive file
Date: 2008-02-17 07:22:21
Message-ID: Pine.GSO.4.64.0802160048110.21338@westnet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 15 Feb 2008, Ubence Quevedo wrote:

> What would the command be to have a query result be put into a location/space
> sensitive file [position 1 through 5 would be one thing where position 6
> through 10 would be the next field, 11 through 16 another, etc]? Is this
> even possible with Postgres?

Check out string operators like rpad and maybe even overlay in
http://www.postgresql.org/docs/current/static/functions-string.html , the
date/number formatting operations in
http://www.postgresql.org/docs/current/static/functions-formatting.html ,
and note that you can combine strings with || in between them.

You can do something like this:

select rpad(field1,5) || rpad(field2,5) || rpad(field3,5) from table1

To get back a string formatted the way you describe. You may have to
tweak that to get the formatting on the fields exactly right. Will be
kind of brute force and fragile if anything changes, but it should work if
you tinker enough.

--
* Greg Smith gsmith(at)gregsmith(dot)com http://www.gregsmith.com Baltimore, MD

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stefan Niantschur 2008-02-17 12:40:29 How to return a large String with C
Previous Message Tom Lane 2008-02-17 02:40:37 Re: Strict-typing benefits/costs