sandesh Jadhav
sandesh Jadhav

Reputation: 339

how to generate auto increment column which will be have some pattern in postgresql?

I have a table named dept_registration it has a column dept_code so I have to make this field auto-generate but in a specific pattern.

example:- test0001 test0002 test0003 test0004

the "test" should be appended before number automatically after insertion

Upvotes: 0

Views: 134

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 248295

The column definition could be

dept_code text DEFAULT 'test' || lpad(nextval('myseq')::text, 4, '0')

where myseq is a sequence.

You will get in trouble once the counter reaches 10000...

Upvotes: 2

Related Questions