REPLACE()
Overview
The REPLACE()
function looks for and replaces a substring with a new one in a string. This function is often used to update the outdated or spelling mistakes in data that require an amendment.
REGEXP_REPLACE()
function. It enables you to search and replace a substring that matches with a POSIX regular expressionSyntax
The syntax for REPLACE()
function is as follows:
REPLACE()
function performs a case-sensitive replacementParameters
The syntax requires three parameters, explained below:
string
: string that you want to replaceold_substring
: substring that you want to replace (all parts will be replaced if it appears multiple times in the string)new_substring
: new substring that will replace the old one
Examples
Basic usage
In this example we will focus on a basic usage of the REPLACE()
function, so we can understand on real example how it works.
The REPLACE()
function will find all occurrences of the ‘New’ substring in the ‘NewDatabase’ string and replace it with the ‘Oxla’ substring, producing the following output:
Replacing specified values in a table
This example shows how to replace the values of a specific column in a table. For the needs of this example, we will create a new table named extracurriculars with club and category columns and insert the values into the respective columns.
Once that is done, we can retrieve all values from the table using the following query:
What we would do here is to replace the ‘group’ values in the category column with ‘sports’:
Removing a substring from a stirng
In the following example, we will show how to remove a substring from a string using the REPLACE()
function. In this case we want to find all occurences of ‘Friends’ substring in ‘Hello Friends’ string and get rid of it:
Replacing multiple patterns
The following example uses the REPLACE()
function to replace multiple patterns of the given string:
We can see that the REPLACE()
function is called multiple times to replace the corresponding string as specified:
[]
into()
{}
into()