How REGEXP_REPLACE() Works in PostgreSQL

In PostgreSQL, we can use the regexp_replace() function to replace a substring within a given string, based on a given POSIX regular expression. We can specify that all matches are replaced or just the first match.

We pass the string as the first argument, the pattern as the second, and the replacement text as the third argument. We also have the option of specifying the start position as the fourth argument, and we can specify a flag to determine how the function behaves.

Continue reading

Understanding PostgreSQL’s TRANSLATE() Function

PostgreSQL has a translate() function which replaces certain characters in a string with certain other characters.

We specify the characters in the string that we want to replace, as well as the characters that we want to replace them with. The function then performs the translation and returns the string with the replaced characters.

Continue reading

5 String Functions that Return Length in PostgreSQL

PostgreSQL provides us with a handful of string functions that return the length of a given string.

But the result between these functions can be different, depending on which function we use. That’s because the “length” can be different, depending on what we’re measuring. Are we talking about the number of characters in the string? Or the number of bytes in the string? Or perhaps we want to know the number of bits in the string.

The function we use will depend on which of the above we’re trying to measure. Below are five functions that cater for each of the above questions.

Continue reading

Using 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 reading

How REGEXP_SPLIT_TO_TABLE() Works in PostgreSQL

In PostgreSQL, the regexp_split_to_table() function splits a string using a POSIX regular expression as the delimiter, and returns the result in a table.

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 change the behaviour of the function.

Continue reading