How do I use generate_series() in SQL in the middle of a string?

Ex:'This is the 1 string.''This is the 2 string.''This is the 3 string.'

I'm looking to generate numbers from 1 to 10000, so I know I would use generate_series(1, 10000), but how can I use this in the middle of a string?

I'm trying to insert this into table 'xyz' column 'strings'.

I have tried this code but it obviously gave me an error:INSERT INTO xyz (strings) 'This is the ' || generate_series(1, 10000) || ' string.';

2

Best Answer


Here is a way to do generate numbers(20) and insert each one in a separate row.

create table t(x varchar(100))insert into t select 'This is ' || cast(x.* as varchar(20)) || ' string'from generate_series(1,20) x

https://dbfiddle.uk/?rdbms=postgres_14&fiddle=b646bd97e9aa3a8bb131dbb0335953ca

To complement what George Joseph and Simon Perepelitsa answered, the final SQL would look like:

insert into xyz(strings) (select 'This is ' || x || ' string' from generate_series(1,20) x)

Hope This Help!