How to Replace Part of a String in MySQL?
Last Updated :
21 Jun, 2024
To replace a part of a string in MySQL we use the REPLACE function. MySQL provides this method to manipulate string data in the tables. In this article, we are going to see how we can update a part of the string with some other value in MySQL. Understanding this can enable string manipulation in a much more complex fashion.
MySQL REPLACE String Function
The REPLACE function in MySQL is used to replace all occurrences of a specified substring within a string with another substring. It is case-sensitive, meaning it distinguishes between uppercase and lowercase characters during replacement.
Note: MySQL REPLACE function performs case-sensitive replacements
Syntax:
REPLACE(string, old_substring, new_substring)
Parameters:
- string: The string in which to replace.
- old_substring: The substring to be replaced.
- new_substring: The substring to replace the old substring.
Simple String Replacement
The following query replaces "World" with "GeeksforGeeks"
SELECT REPLACE("Hello World!", "World", "GeeksforGeeks") AS Greeting;
Output:
Greeting |
---|
Hello GeeksforGeeks! |
Explanation: The given SQL query utilizes the REPLACE
function to modify the string "Hello World!" by replacing every occurrence of the substring "World" with "GeeksforGeeks". The resulting output assigned the alias "Greeting", is "Hello GeeksforGeeks!" – a string where the specified substitution has been applied, effectively altering the original greeting.
MySQL Replace Part of String in Column Example
We have covered how to use the REPLACE function to change the part of a string in MySQL. Now let's see how to do the same when the string is in a column of MySQL table.
Let's start by creating a table and adding some sample data to the table. We create an EMPLOYEE table which contains fields like empId, name, and the email of the person. The following query creates the table:
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
);
INSERT INTO EMPLOYEE VALUES
(0001, 'Clark', '[email protected]');
(0002, 'Dave', '[email protected]');
(0003, 'Ava', '[email protected]');
Output:
To replace a part of string in column, we will use REPLACE function with UPDATE clause.
Example of Using REPLACE Function with UPDATE Clause
Replace 'some.com' to 'domain.net' in email column of EMPLOYEE table
UPDATE EMPLOYEE SET email=REPLACE(email, 'some.com', 'domain.net');
The following is the data of the table after executing the above query:
Output:
Explanation: The email domain for each employee has been updated from 'some.com' to 'domain.net'. The REPLACE
function ensures that occurrences of 'some.com' in the email column are replaced with 'domain.net'. As you can see the email of each employee has changed from [email protected] to [email protected].
Example of Updating Product Descriptions
We will create a PRODUCTS
table with fields like productId
, productName
, and description
.
CREATE TABLE PRODUCTS (
productId INTEGER PRIMARY KEY,
productName TEXT NOT NULL,
description TEXT NOT NULL
);
INSERT INTO PRODUCTS (productId, productName, description) VALUES
(1, 'Laptop', 'High performance laptop with 16GB RAM and 512GB SSD'),
(2, 'Smartphone', 'Latest model smartphone with 5G and 128GB storage'),
(3, 'Tablet', 'Lightweight tablet with 10-inch display and 64GB storage');
Query:
UPDATE PRODUCTS SET description = REPLACE(description, 'GB', 'Gigabytes');
Output:
productId | productName | description |
---|
1 | Laptop | High performance laptop with 16Gigabytes RAM and 512Gigabytes SSD |
2 | Smartphone | Latest model smartphone with 5G and 128Gigabytes storage |
3 | Tablet | Lightweight tablet with 10-inch display and 64Gigabytes storage |
Conclusion
The REPLACE function is a powerful tool for string manipulation in MySQL. It allows you to replace parts of strings within a table column efficiently. Using the REPLACE function with the UPDATE statement enables you to perform bulk updates on string data, making it a crucial function for database administrators and developers. By mastering this function, you can handle various string manipulation tasks, from simple replacements to complex modifications, with ease.
Similar Reads
How to UPDATE and REPLACE Part of a String in MariaDB
MariaDB is one of the most popular open-source database systems. It is developed by the developers of MySQL. In this article, we will How to UPDATE and REPLACE part of a string in MariaDB along with various examples and methods and so on. MariaDB REPLACE String FunctionThe REPLACE function is used t
3 min read
How to replace String in PHP ?
Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
3 min read
How to UPDATE and REPLACE Part of a String in SQLite
In SQLite, updating and replacing parts of a string can be a common task, especially when dealing with textual data. SQLite, serverless architecture offers various methods to solve this problem. In this article, We will learn about string replace in a query with the help of various methods to know h
4 min read
How to UPDATE and REPLACE Part of a String in PL/SQL?
PL/SQL is a procedural language designed to enable developers to combine the power of procedural language with Oracle SQL. It is developed by Oracle and serves as one of the three key programming languages embedded in the Oracle database, alongside SQL and Java. PL/SQL includes procedural language e
3 min read
How to UPDATE and REPLACE Part of a String in SQL Server
In SQLServer, efficient manipulation of strings is crucial for managing databases effectively. Among the fundamental operations are updating and replacing parts of strings within tables. These operations are invaluable for correcting data inconsistencies, enhancing data quality, and transforming tex
4 min read
How to Use str_replace in R?
str_replace() is used to replace the given string with a particular value in R Programming Language. It is available in stringr library, so we have to load this library. Syntax: str_replace( "replacing string", "replaced string") where, replacing string is the string to be replacedreplaced string is
2 min read
How to Escape a Single Quote in MySQL
MySQL is an open-source relational database management system (RDBMS) that is widely used for managing and organizing structured data. MySQL is designed to run on various operating systems, including Windows, Linux, macOS, and others, providing flexibility in deployment. It can handle databases of d
5 min read
How to replace multiple characters in a string in PHP ?
A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP. Using the str_repl
3 min read
SQL Query to Match Any Part of String
It is used for searching a string or a sub-string to find a certain character or group of characters from a string. We can use the LIKE Operator of SQL to search sub-strings. The LIKE operator is used with the WHERE Clause to search a pattern in a string of columns. The LIKE operator is used in conj
3 min read
How to replace a portion of strings with another value in JavaScript ?
In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca
3 min read