Question: Generate serie with prefix and leading zeros (postgresql)

Question

Generate serie with prefix and leading zeros (postgresql)

Answers 2
Added at 2016-12-22 16:12
Tags
Question

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!

Answers to

Generate serie with prefix and leading zeros (postgresql)

nr: #1 dodano: 2016-12-22 16:12
SELECT 'A'||lpad(s::text,5,'0') as myval FROM generate_series(8,12) as t(s)
nr: #2 dodano: 2016-12-22 16:12

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

Source Show
◀ Wstecz