Re: Storing number '001' ?

From: Jason Earl <jason(dot)earl(at)simplot(dot)com>
To: Charles Hauser <chauser(at)acpub(dot)duke(dot)edu>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Storing number '001' ?
Date: 2001-12-06 18:35:45
Message-ID: 871yi8b3i6.fsf@npa01zz001.simplot.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


Actually you probably don't want to *store* those extra zeros, you
simply want to display them.

So simply make sure that you use lpad when you select these values.
For example if I had a table test like this:

CREATE TABLE test (
foo integer
);

INSERT INTO test (foo) VALUES (1);
INSERT INTO test (foo) VALUES (2);
INSERT INTO test (foo) VALUES (3);
INSERT INTO test (foo) VALUES (300);

I could then select from it with a query like:

SELECT lpad(foo::text, 3, '0') AS padded_num FROM test;

and get:

padded_num
------------
001
002
003
300
(4 rows)

Neat huh! Of course, you also might want to put a constraint on that
column because otherwise you could have problems. For example if you
add another value to the table:

INSERT INTO test (foo) VALUES (3000);

and then select use the lpad query you get:

padded_num
------------
001
002
003
300
300
(5 rows)

Which could be bad.

Another tactic would be to simply store the value as a char(3) or
varchar(3) value and do your error checking when you insert the value.
If you are going to be doing math on these values, however, it is
probably a win to store the value as an integer.

Jason

Charles Hauser <chauser(at)acpub(dot)duke(dot)edu> writes:

> Hi,
>
> I need to store numbers which contain '0' as the first digit like
> '001', '01' .
>
> A little history. A DNA clone_id is denoted by '894001A01.x1'. I
> need to sort clone_id, and have broken it down into its meaningful
> components:
>
> project: 894
> plate: 001
> plate row: A
> plate column: 01
> read: x
> ver: 1
>
> CREATE TABLE clone_fasta (
> project integer NOT NULL,
> plate integer NOT NULL,
> p_row char(1) NOT NULL,
> p_column integer NOT NULL,
> read char(1) NOT NULL,
> ver integer NOT NULL,
> length integer NOT NULL,
> seq text NOT NULL,
> PRIMARY KEY (project,plate,p_row,p_column,read,ver)
> );
>
> Unfortunately, storing these numbers as integers converts 001 ->1,
> which I can't use.
>
> How does one store a number like '001'?
>
> Thanks,
> --
> Chuck
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Leandro Fanzone 2001-12-06 19:12:36 Mirror databases
Previous Message Josh Berkus 2001-12-06 16:59:18 Re: Number of days in a month