In PostgreSQL, we can use the overlay()
function to replace a substring within a string. The function allows us to be very specific with regards to where in the string to start the operation and how much of it should be replaced.
Difference Between ARRAY_POSITIONS() and ARRAY_POSITION() in PostgreSQL
PostgreSQL has a couple of functions that enable us to get the position or positions of a given value in an array. One of them – array_position()
– returns the position of just the first occurrence (as an integer), while the other function – array_positions()
– returns the position of all occurrences (as an array).
So these functions are aptly named – one is singular and the other is plural.
But this isn’t the only difference. The array_position()
function allows us to specify a start position, while array_positions()
doesn’t.
Fix Error “op ANY/ALL (array) requires array on right side” in PostgreSQL
If you’re getting a PostgreSQL error that reads “op ANY/ALL (array) requires array on right side” it’s probably because you’re passing a non array to the ANY()
or ALL()
construct.
These constructs require an array to be included in the parentheses, and so passing a non array returns an error.
To fix this issue, be sure to pass an array when using these functions.
Continue readingUsing LTRIM() in PostgreSQL
In PostgreSQL, the ltrim()
function trims the left side 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.
Continue readingUnderstanding RTRIM() in PostgreSQL
In PostgreSQL, we can use the rtrim()
function to trim the right side 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.
Continue readingA Quick Look at the TRIM() Function in PostgreSQL
In PostgreSQL, the trim()
function can be used to trim both sides, or a specified side, 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.
Continue readingFix Error “date field value out of range” when using make_date() in PostgreSQL
If you’re getting an error that reads ‘date field value out of range‘ in PostgreSQL while using the make_date()
function, it’s probably because one or more of the date parts you’re providing is out of range for that date part.
It’s possible that you’ve got the month and day in the wrong order.
To fix this issue, be sure that each date part you provide is within the valid range for that date part. It may be that all you need to do is switch the day and the month around.
Continue readingLet’s Look at PostgreSQL’s ARRAY_REMOVE() Function
We can use the array_remove()
function in PostgreSQL to remove elements from an array.
We pass two arguments when calling the function; the first is the array and the second argument is the element to remove. All occurrences of the specified element are removed.
The function returns an identical array, but without the specified element/s.
Continue readingHow to Make a Zero-Based Array in PostgreSQL
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 readingUnderstanding 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.