REPLACE() Function in MySQL
Last Updated :
24 Sep, 2024
The REPLACE()
function in MySQL is a powerful tool for string manipulation, allowing users to substitute specific substrings within a larger string.
This functionality is particularly useful in various applications such as updating text data, cleaning up input or adjusting content in a database. In this article, We will learn about the REPLACE() Function in MySQL by understanding various examples.
REPLACE() Function in MySQL
The REPLACE()
function in MySQL is used to replace all occurrences of a specified substring with another substring within a given string.
Syntax:
REPLACE(string, from_substring, to_substring)
Parameter:
- string: The original string.
- from_substring: The substring to be replaced.
- to_substring: The substring to replace with.
Examples of REPLACE() Function in MySQL
Example 1
In this example, we will see how we can replace any string value with a new string value by using the replace function. Let's consider an example where "MySQL" replace with "HTML" value.
Below given is the replace function values.
Replace "MySQL" with "HTML"
Now, we will see how we can read the replace values.
SELECT REPLACE("MySQL in Geeksforgeeks", "SQL", "HTML");
Output:
REPLACE("MySQL in Geeksforgeeks", "MySQL", "HTML") |
---|
HTML in Geeksforgeeks |
Example 2
Let's update a string by replacing all occurrences of the letter "X" with the letter "A"
Replace "X" with "A"
Now, If we want to read the replaced value then use the following function given below.
SELECT REPLACE("It is Good to study XXX from GFG", "X", "A");
Output:
REPLACE("It is Good to study XXX from GFG", "X", "A") |
---|
It is Good to study AAA from GFG |
Example 3
Let's replace all occurrences of the letter "x" in a given string with the letter "a".
Replace "x" with "a"
Now, If we want to read the replaced value then use the following function given below.
SELECT REPLACE("It is Good to study xxx from GFG", "x", "a");
Output:
REPLACE("It is Good to study xxx from GFG", "x", "a") |
---|
It is Good to study aaa from GFG |
Conclusion
The REPLACE()
function in MySQL is a valuable tool for modifying text strings by replacing specified substrings with new ones. In our example, replacing "x" with "a" in the phrase "It is Good to study xxx from GFG" results in "It is Good to study aaa from GFG." This functionality is essential for tasks such as data cleaning, content updating, and enhancing overall data quality in databases, making it a key component of effective string manipulation.
Similar Reads
SPACE() Function in MySQL SPACE() function in MySQL is used to return a string consisting of specified empty space characters. Syntax : SPACE(num) Parameter : This method accepts one parameter as mentioned above and described below : num : It is an integer which indicates how many spaces are being contained. Returns : It ret
1 min read
RPAD() Function in MySQL RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
1 min read
PLSQL | REPLACE Function The PLSQL REPLACE function is used for replacing a sequence of characters in a string with another set of characters. The REPLACE function accepts three parameters which are input_string, string_to_replace and replacement_string. The REPLACE function returns input_string with every occurrence of str
2 min read
YEAR() Function in MySQL YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
ORD() Function in MySQL ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
TAN() Function in MySQL TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read