Re: pad column with leading zeros or space

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "Johnson, Shaunn" <SJohnson6(at)bcbsm(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: pad column with leading zeros or space
Date: 2002-01-23 17:58:23
Message-ID: 20020123094421.L19046-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 23 Jan 2002, Johnson, Shaunn wrote:

In general, Tom's right that it's probably best to do this in the front
end, but...

> create table foo (
> member_id lpad(10, '0')
> );
>
> [/snip guess]
>
> I know this doesn't work, but that's sorta what I'm going for.

If you want it to really be stored with all the 0's, a trigger
would probably do what you want. If you want it converted
to the text string with 0's when a select is done, you'd probably
want a view to do the manipulation for you.

(trigger example - mostly untested:)
create table foo (
member_id text
);

create function foofunc() returns opaque as
'begin
NEW.member_id := lpad(NEW.member_id, 10, ''0'');
return NEW;
end;'
language 'plpgsql';

create trigger t1 before insert or update on foo
for each row execute procedure foofunc();

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-01-23 18:01:19 Re: postgresql 7.2b5 and vserver: statistics sockets
Previous Message Tom Lane 2002-01-23 17:54:11 Re: persistent portals/cursors (between transactions)