How to create array with range in postgresql?

by wilmer.lemke , in category: MySQL , 3 months ago

How to create array with range in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , a month ago

@wilmer.lemke 

In PostgreSQL, you can create an array with a range using the generate_series function. Here's an example of how to create an array with a range of integers from 1 to 10:

1
SELECT ARRAY(SELECT generate_series(1, 10));


This will create an array with the values {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.


You can also specify a step value for the range by providing it as the third argument to generate_series. For example, to create an array with a range of even numbers from 2 to 10:

1
SELECT ARRAY(SELECT generate_series(2, 10, 2));


This will create an array with the values {2, 4, 6, 8, 10}.