In PostgreSQL, we can use the ||
operator to concatenate two arrays. We can also use it to append or prepend a (compatible) non-array value to an array.
Tag: arrays
How to Update an Array in PostgreSQL
PostgreSQL allows us to create arrays and store them in the database as an array type. We can then go back later and update those arrays as required.
We can either replace the whole array, append or prepend elements, or update individual elements within the array. When we update individual elements, we can update one element or a whole slice. Below are examples of updating an array in PostgreSQL.
Continue readingHow to Specify your own Subscript Range when Creating an Array in PostgreSQL
By default, PostgreSQL arrays are one-based. This means that we need to use 1
if we want to reference the first element in the array, 2
for the second, and so on.
But we also have the option of specifying our own subscript range for an an array. For example we could create a zero-based array, a ten-based array, or even a negative value such as a negative ten-based array.
We can do this by using subscripted assignment to specify the actual subscript range for the array. Basically, we prefix the array with the subscript range, enclosed in square brackets, and an equals sign (=
) between it and the array.
So PostgreSQL ARRAY_APPEND() Works but ARRAY_PREPEND() Doesn’t? Try this.
If you’re updating arrays in PostgreSQL and you’ve suddenly realised that some of the arrays aren’t being updated, it could be due to the following.
If you’ve been using the array_append()
function and the array_prepend()
function, you may have found that one function works but the other doesn’t. For example array_append()
works but array_prepend()
doesn’t, or vice-versa.
Create a Multi-Dimensional Array from a Query in PostgreSQL
In PostgreSQL, we can use array constructors to create arrays that contain elements that we specify. We can populate the array from literal values, or we can let a query provide the values.
We can also create multi-dimensional arrays. When populating the array from a query, we can use multiple subqueries to create each sub-array. Therefore we can create a multi-dimensional array from a query.
Continue readingUsing REGEXP_SPLIT_TO_ARRAY() in PostgreSQL
In PostgreSQL, the regexp_split_to_array()
function splits a string using a POSIX regular expression as the delimiter, and returns the result in a text array.
So we use a POSIX regular expression to specify the delimiter/s, and split the string based on that.
We pass the string as the first argument and the pattern as the second. We can also specify a flag to determine how the function behaves.
Continue readingCreate an Array from a Query in PostgreSQL
In PostgreSQL, an array constructor is an expression that builds an array value using values for its member elements.
One of the things we can do is build an array based on the results of a subquery.
Continue readingFix “function array_shuffle(numeric) does not exist” in PostgreSQL
If you’re getting an error that reads “function array_shuffle(numeric) does not exist” when using the array_shuffle()
function in PostgreSQL, it’s probably because the argument you’re passing to the function is not an array.
More specifically, this error message implies that you’re passing a numeric type. The same error can occur when passing other non-array types (such as integer), but the solution is the same.
The argument for this function must be an array.
To fix this error, replace the numeric value with an array value when calling the array_shuffle()
function.
Using ARRAY_REPLACE() in PostgreSQL
In PostgreSQL we can use the array_replace()
function to replace a specified element (or elements) in an array with another element.
The first argument is the array, the second is the element to replace, and the third is the element to replace it with.
Continue readingA Quick Look at TRIM_ARRAY() in PostgreSQL
In PostgreSQL, we can use the trim_array()
function to trim a given number of elements from the end of an array.
The first argument is the array, and the second is the number of elements to trim.
Continue reading