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 readingIn 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 readingIf 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.
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 readingIn 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 readingIf you’re getting an error that reads “cannot determine type of empty array” in PostgreSQL, it could be that you’re trying to create an empty array without specifying the array type.
It’s impossible to create an array with no type, and so if we want to create an empty array, then we need to specify the type or add items to determine the type.
To fix this issue, either specify the array type for the empty array, or add items to the array.
Continue readingWe have several options when it comes to appending elements to arrays in PostgreSQL. We can use an operator to concatenate the value to the array or we can use a function to do the job.
Below are four ways to append elements to arrays in PostgreSQL.
Continue readingIn PostgreSQL we can use the array_fill()
function to create an array filled multiple instances of a given value.
We pass the value to populate as the first argument, followed by the length of the desired array. The resulting array replicates the first argument as specified by the length in the second argument.
We also have the option of passing a third argument to specify the lower bound values of each dimension of the array.
Continue readingIn PostgreSQL we can use the array_to_string()
function to convert an array to a string. It converts each array element to its text representation (if needed), and then concatenates all elements using the given delimiter.
Any null values are omitted from the result, but we do have the option of replacing any null values with a given text value.
Continue readingIn PostgreSQL, we can use the unnest()
function to expand an array into a set of rows. We pass the array as an argument, and the function returns each element on a separate row.
We can use the function on multi dimensional arrays, and it’s also possible to use it to unnest multiple arrays by including it in the FROM
clause of a query.
In PostgreSQL, string_to_array()
is a string function that allows us to create an array from a string. It splits the string based on the specified delimiter and returns a text
array as a result.
If we specify a null delimiter, then each character becomes an element in the array. If the delimiter string is empty, then the whole string becomes a single element in the array.
We also have the option of turning a specific substring into null
if required.