SQL Server REPLACE() Function
Last Updated :
12 Apr, 2024
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.
REPLACE() Function
In SQL Server, the REPLACE() function is used to modify a string value. It is used to replace all the occurrences of a substring with a given string. We generally use the REPLACE() function along with the SELECT statement or UPDATE statement. It is also a case insensitive. This means it replaces all the occurrences of a given substring within a string regardless of its case.
Syntax:
REPLACE ( Input String, S1 , S2)
- S1: Substring to be replaced within the given string.
- S2: Replacement Substring.
Demo SQL Server Database
In this tutorial on REPLACE() function, we will use the following table for examples.
Table geeksforgeeksTo create this table in your system, you have to run the below query:
Query:
--to create the table
CREATE TABLE [geeksforgeeks]
(
[id] [int] UNIQUE,
[name] [varchar](20),
[course] [varchar] (50)
) ON [PRIMARY]
--to insert values to the table
INSERT INTO geeksforgeeks ([id],[name],[course])
VALUES (1, 'Vishu', 'Python Self Paced');
INSERT INTO geeksforgeeks ([id],[name],[course])
VALUES (2, 'Neeraj', 'Java Self Paced');
INSERT INTO geeksforgeeks ([id],[name],[course])
VALUES (3, 'Aayush', 'SQL Self Paced');
INSERT INTO geeksforgeeks ([id],[name],[course])
VALUES (4, 'Sumit', 'Java Self Paced');
INSERT INTO geeksforgeeks ([id],[name],[course])
VALUES (5, 'Vivek', 'Java Self Paced');
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:
replace()with selectExplanation: 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 geeksforgeeks
SET course = REPLACE(course, 'Java', 'C++')
WHERE course LIKE '%Java%';
Output:
Replace () with an update statementExplanation: 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.
REPLACE() Function as CASE-Sensitive
As stated earlier, REPLACE function is case insensitive. In some of the cases, we want only some particular case data to be replaced say it to be upper case or lower case. We can solve this problem very easily. We will use binary collation. Doing this, REPLACE() function will treat characters differently based on their binary values.
Query:
SELECT REPLACE('JAVA is good' COLLATE Latin1_General_BIN, 'java', 'Python') as RESULT
Output:
Replace as case-sensitiveExplanation: In the above image, you can see that the input string remains unchanged. We have specified 'java' in lower case but in upper case, JAVA is present in the string. To replace the word, we need to pass the exact word keeping the case unchanged.
Query:
SELECT REPLACE ( 'JAVA is good' COLLATE Latin1_General_BIN, 'JAVA' , 'Python') as RESULT
Output:
Replaced StringExplanation: We can see that the string has been updated now. In this case, we have specified the same case as in the given string string. Therefore the substring string has been matched and get replaced by our REPLACE() function.
Conclusion
Overall, the REPLACE() Function is used to replace a substring within the given string. It is a case-in-sensitive function. It is used in various real-life applications. One of its real-life applications is data cleaning. REPLACE() is not a standalone function. Therefore, we generally use the REPLACE() function with UPDATE or SELECT Statement. We have covered examples related to replace functions along with how we can use the REPLACE() function as case-sensitive. Now you have a good understanding of the REPLACE() function, you can build queries related to it and get the desired result.
Similar Reads
REVERSE() Function in SQL Server
The REVERSE() function in SQL Server is a simple and powerful tool designed to reverse the order of characters in a string. By taking a string input, it returns a new string with its characters arranged in the opposite sequence. In this article, We will learn to REVERSE() Functions in SQL Server by
3 min read
RANK() Function in SQL Server
The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
5 min read
SQL Server ISNULL() Function
The ISNULL() function in SQL Server is a powerful tool for handling NULL values in our database queries. It allows us to replace NULL values with a specified replacement value, ensuring that your queries return meaningful results even when data is missing. In this article, We will learn about the SQ
4 min read
Replace String in SQL Server
In SQL Server, manipulating data from a table and applying functions to modify values dynamically is a common task. One such example involves replacing parts of strings, such as email domains. This content demonstrates how string replacement can be applied to a dataset that includes employee informa
2 min read
SQL Server CAST() Function
In SQL Server, manipulating data is a fundamental aspect of database management. Often, you'll find yourself needing to transform data from one type to another, either for calculations, comparisons, or presentation purposes. This is where the CAST() function comes. In this article, we will learn abo
3 min read
Rename Column in SQL Server
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods
3 min read
YEAR() Function in SQL Server
The YEAR() function in SQL Server is a powerful tool designed to extract the year component from a given date or datetime expression. It allows users to isolate the year as an integer value and facilitating various date-related operations and analyses. In this article, We will learn about the YEAR()
2 min read
SQL Server | STUFF() Function
The STUFF() function in SQL Server is a powerful string manipulation tool used to delete a specified length of characters from a string and insert another set of characters at a given starting position. This function becomes particularly useful in scenarios where complex string operations are requir
2 min read
UPPER() function in SQL Server
The UPPER() function in SQL Server is a useful tool for converting all characters in a string to uppercase. This function is essential for ensuring uniform text formatting and for performing case-insensitive comparisons. In this article, We will learn about the UPPER() function in SQL Server by unde
3 min read
SQL Server SPACE() function
The SQL Server SPACE() function is a handy tool for generating a string of space characters. It is particularly useful for formatting and padding data within SQL queries. By specifying the number of spaces, users can ensure consistent spacing in output results, which can be critical for aligning tex
3 min read