

regular_expr_pattern: This is the regular expression pattern that will be searched for possible matched substrings to be replaced.original_string: This source string contains the words of substrings matching the regular expression that needs to be replaced.Syntax: REGEXP_REPLACE(original_string, regular_expr_pattern, new_sub_string ) This can be the case when replacing the old phone numbers or email ids with some default value with some new value. When we want to replace the words matching a particular regular expression to another word, we can use REGEXP_REPLACE() Function in PostgreSQL. The replace() Function is available and compatible with the following versions of PostgreSQL: Now we will see how we can replace the strings matching the regular expressions to be replaced by some other string.

In this way, we studied firstly how a single occurrence of the word can be replaced, then multiple occurrences of the word were replaced, and finally, a substring replacement and table column data substring replacement demonstration with the help of an example. Hence the columns that didn’t have that substring don’t result in any error but are just skipped. We will replace the department string’s substring’ -side language’ with ‘-end technology’ using the following command.Īs we can see, four rows are updated, but only two of them had that substring. Now, let us see the contents by firing the following query statement. Let us fire \dt command to see all the tables present in my database. Now, we will replace the substring of the particular column of the table with some other substring. The sky is warm and sunny in the morning.','The sky is','Breeze and air are') We will again use the replace() method to do this, and our query statement will be as follows.Ĭode: select replace('The sky is beautiful and calm at night. The sky is warm and sunny in the morning.” and replace the substring “The sky is” with the “Breeze and air are” substring. Consider a string “The sky is beautiful and calm at night. Instead of replacing the word, let us replace the substring in the original string. The REPLACE() method replaces both occurrences of the word ‘this’ with the word ‘that’ in the given string. For this, we will use the following query statement.Ĭode: select replace('We can do this. We shall do this.’ This is the sentence in which we want to replace the “this” word with the “that” word. Select replace('The waves of the sea help us to get back to ourselves.', 'sea', 'ocean')
