Replace String in SQL Server
Last Updated :
27 Sep, 2024
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 information such as name, salary, city, and email address.
Replacing String Values in SQL Server
- When we need to update or replace any string value in a table in SQL Server, we can use various methods.
- Below are examples demonstrating how to replace a part of a string using the
REPLACE()
function in SQL Server.
Example 1: Replacing a String in a Variable
In this example, we will replace a part of a string stored in a variable with a new string using the REPLACE()
function.
DECLARE @String_Value varchar(50)
SET @String_Value = 'This provides free and excellent knowledge on SQL Server.'
SELECT REPLACE(@String_Value, 'This', 'GeeksforGeeks');
Output:
GeeksforGeeks provides free and excellent knowledge on SQL Server.
Example 2: Replacing Strings in a Table
Consider the following table named geek_demo
:
Example 3: Replacing a String in a SELECT
Statement
In this example, we will replace the domain of email addresses from 'xyz.com' to 'gfg.org' while selecting data from the geek_demo
table using the REPLACE()
function.
SELECT [Name], [Salary], [City], [email],
REPLACE([email], 'xyz.com', 'gfg.org') AS [New EmailID]
FROM [geek_demo];
Output:
Example 4: Replacing Strings in an UPDATE
Statement
To update the email addresses in the geek_demo
table, we can use the REPLACE()
function inside an UPDATE
statement:
UPDATE [geek_demo]
SET [email] = REPLACE([email], 'xyz.com', 'gfg.org');
Result:
(8 row(s) affected)
Output:
Conclusion
Modifying string values in a dataset is an essential skill when working with SQL Server. By applying functions like REPLACE()
, you can efficiently update strings such as email addresses, which may be necessary when changing domain names or correcting typos.
Similar Reads
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 proces
4 min read
SQL Server SELECT INTO Statement SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applic
6 min read
SQL Server SUBSTRING() Function The SQL Server SUBSTRING function extracts a substring from a string, starting at a specified position and with an optional length. The SUBSTRING function also works in Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse. SyntaxThe SQL SUBSTRING function syntax is: SUBSTRING(in
3 min read
Using REPLACE Function in SQL In Structured Query Language (SQL), the REPLACE function is used to replace a substring or a part of a string within the given String. While dealing with some data pre-processing tasks or some data cleaning tasks, the REPLACE function is found to be very useful. It can save a lot of time and it also
4 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 Insert Line Break in SQL Server String? In SQL Server there are various datatypes like int, float, char, nchar, etc but especially while we are dealing with text in VARCHAR and NVARCHAR columns, we might run into situations where we need to make the text look cleaner by adding line breaks. This could be for better organization, and readab
4 min read