Thread: Sequence Help

Sequence Help

From
"Brian C. Doyle"
Date:
Hello all,

How would I setup a sequence that would reflect, for example, abc-0001?




Re: Sequence Help

From
"Robert B. Easter"
Date:
On Wednesday 17 January 2001 14:53, Brian C. Doyle wrote:
> Hello all,
>
> How would I setup a sequence that would reflect, for example, abc-0001?

reaster=# create sequence bb;
CREATE

reaster=# select 'abc-' || lpad(nextval('bb'), 4, '0');
 ?column?
----------
 abc-0001
(1 row)

lpad truncates on the right if the string number gets longer than 4 digits.

You'll have to create the sequence, then do something like:

reaster=# create table ttt ( id text default ('abc-' || lpad(nextval('bb'),
4, '0')) );
CREATE
reaster=# insert into ttt default values;
INSERT 2836131 1
reaster=# insert into ttt default values;
INSERT 2836132 1
reaster=# insert into ttt default values;
INSERT 2836133 1
reaster=# insert into ttt default values;
INSERT 2836134 1
reaster=# insert into ttt default values;
INSERT 2836135 1
reaster=# insert into ttt default values;
INSERT 2836136 1
reaster=# insert into ttt default values;
INSERT 2836137 1
reaster=# select * from ttt;
    id
----------
 abc-0013
 abc-0014
 abc-0015
 abc-0016
 abc-0017
 abc-0018
 abc-0019
(7 rows)


--
-------- Robert B. Easter  reaster@comptechnews.com ---------
-- CompTechNews Message Board http://www.comptechnews.com/ --
-- CompTechServ Tech Services http://www.comptechserv.com/ --
---------- http://www.comptechnews.com/~reaster/ ------------