sibert
sibert

Reputation: 2228

Generate serie with prefix and leading zeros (postgresql)

I am trying to generate a serie that contains both prefix ('A') and leading zeros.

SELECT * FROM generate_series(8,12)

will generate a serie

8
9
10
11
12

But I want to prefix and set max 4 leading zeros

A00008
A00009
A00010
A00011
A00012

Is this possible to do with a Postgresql query?

TIA!

Upvotes: 1

Views: 782

Answers (2)

user330315
user330315

Reputation:

You can use to_char() to format the number:

SELECT to_char(i, 'FMA00000') as x
FROM generate_series(8,12) as t(i)

For details on to_char() please see the manual

Upvotes: 3

JosMac
JosMac

Reputation: 2302

SELECT 'A'||lpad(s::text,5,'0') as myval FROM generate_series(8,12) as t(s)

Upvotes: 3

Related Questions