Re: Formatting Function.. - Mailing list pgsql-general

From Rolf Østvik
Subject Re: Formatting Function..
Date
Msg-id Xns951D6DABF5B03rolfostvikjobbyahoon@200.46.204.72
Whole thread Raw
In response to Formatting Function..  (Vinay Jain <vinayjain@gmail.com>)
List pgsql-general
vinayjain@gmail.com (Vinay Jain) wrote in
news:c729bfe0040704221910989de8@mail.gmail.com:

> Hi..
> I am newbe in postgresql so please help me though the question may be
> very easy to answer..
> Is there any formatting function to get output with fix lengths..for
> example my query is..
> schema is:
>
> Student
> (name Varchar,
> age integer);
>
> select name, age from student;
> the output is like this..
> Name                                      | Age
> xyz                                          | 22

I am a little curious about where you want your data to be presented. If it
is only output in psql or other places where text is presented with a fixed
with font then my suggestions will help.
(If the data is to be outputted in html format then there are other
methods.)

One option is to store text data in char coloumns, it will fill the field
with spaces to the left. This i will only recommend in special cases.


Use formatting functions instead
        for text use lpad and rpad
        for numbers use to_char
example:
select rpad(name,20) as 'Name', to_char(age,'9999') as 'Age' from student;

read more about it in the manual (Chapter 9 in the 7.4. documentation)
--
Rolf

pgsql-general by date:

Previous
From: Kazuya Togashi
Date:
Subject: Difference in text/char data matching between 7.3.4 & 7.4.2
Next
From: "B.W.H. van Beest"
Date:
Subject: How to filter on timestamps?