Generate serie with prefix and leading zeros (postgresql)

731 views Asked by At

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!

2

There are 2 answers

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

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