REPLACE() Function Examples
In this, we will various examples along with their respective explanations.
Example 1: REPLACE() Function with SELECT Statement
In this example, we are going to implement the REPLACE() function with SELECT Statement.
Query:
SELECT REPLACE('Java is used in ML', 'Java', 'Python') as Result;
Output:
Explanation: In the query, we have passed a string ‘Java is used in ML’, here ‘Java’ is the substring we want to replace, and ‘Python’ is the replacement substring. We have passed all the required information to the REPLACE() function. As shown in image, ‘Java’ has been replaced with ‘Python’. Now the new string is ‘Python is used in ML’.
Example 2: REPLACE() function with update statement.
In this example, we will see how we can use REPLACE() function in UPDATE statement. We will update column values with the help of REPLACE() function.
Query:
UPDATE w3wiki
SET course = REPLACE(course, 'Java', 'C++')
WHERE course LIKE '%Java%';
Output:
Explanation: In the above image, we can observe that id’s 2,4, and 5 have Java in their course column previously. We have specified in the query that, replace all the occurrences of Java with C++ from the courses where Java Word is present.
SQL Server REPLACE() Function
SQL Server is a strong relational database management system (RDBMS) developed to manage large data efficiently. In SQL Server, the REPLACE() function is used to modify or replace a substring within a given string. Taking about the real word uses, the REPLACE() function is vastly used in data processing tasks such as data cleaning. It is also used for data masking purposes.
In this article, we are going to deep dive into the uses and implementation of the REPLACE() function. We will explore various examples along with their respective explanations.
Contact Us