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.

Continue reading

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 reading