By default, PostgreSQL arrays are one-based. That means that the numbering starts at subscript 1 and increments with each element in the array. However, we can change this so that the array starts at a different subscript.
Continue readingCategory: PostgreSQL
Understanding OCTET_LENGTH() in PostgreSQL
In PostgreSQL, the octet_length()
function returns the number of bytes in a given string. We pass the string as an argument and the function returns the number of bytes in that string.
A Quick Look at the BIT_LENGTH() Function in PostgreSQL
PostgreSQL has a bit_length()
function that returns the number of bits in a given string. We pass the string as an argument and the function returns the number of bits in that string.
Fix Error “function generate_subscripts(numeric, integer) does not exist” in PostgreSQL
If you’re getting a PostgreSQL error that reads “function generate_subscripts(numeric, integer) does not exist“, it’s probably because your first argument is not an array. This specific error message implies that the first argument is a numeric value, but we’d get a similar error when passing an integer.
The first argument for this function must be an array.
To fix this error, be sure that your first argument to generate_subscripts()
is an array.
A Quick Intro to ARRAY_CAT() in PostgreSQL
In PostgreSQL, we can use the array_cat()
function to concatenate two arrays.
We pass both arrays as arguments when we call the function.
Continue readingFix Error “searching for elements in multidimensional arrays is not supported” in PostgreSQL
If you’re getting a PostgreSQL error that reads “searching for elements in multidimensional arrays is not supported“, it’s probably because you’re trying to perform an operation against a multi dimensional array when that operation only supports one dimensional arrays.
To fix this issue, perform the operation against a one dimensional array instead. You may be able to do this by applying the unnest()
function against the multi dimensional array.
Understanding the BTRIM() Function in PostgreSQL
PostgreSQL has a function called btrim()
that we can use to trim both sides of a string. We can trim blank spaces or we can specify certain characters to trim.
It removes the longest string containing only characters from the ones we specify. If we don’t specify any characters, then it removes blank spaces from both sides.
We provide the string as the first argument, and the (optional) characters as the second.
Continue reading4 Ways to Search an Array in PostgreSQL
In PostgreSQL, we have many ways to retrieve data from arrays. One way is to specifically reference its subscript or a range of subscripts. Another way is to search through the array’s contents for the value we want.
By doing a search, I mean we could do things like filter the query with a WHERE
clause, so that we only return rows that contain an array that has an element with a certain value.
How ARRAY_PREPEND() Works in PostgreSQL
In PostgreSQL, the array_prepend()
function prepends an element to the start of an array.
The first argument is the element to prepend, and the second argument is the array.
Continue readingA Quick Look at ARRAY_APPEND() in PostgreSQL
In PostgreSQL, we can use the array_append()
function to append an element to the end of an array.
The first argument is the array, and the second argument is the element to append.
Continue reading