Replace String in SQL Server Last Updated : 27 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 ServerWhen 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 VariableIn 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 TableConsider the following table named geek_demo:NameSalaryCityEmailAnkit24500Delhi[email protected]Babita23600Noida[email protected]Chetan25600Noida[email protected]Deepak24300Delhi[email protected]Isha25900Delhi[email protected]Khushi24600Noida[email protected]Megha25500Noida[email protected]Parul23900Noida[email protected]Example 3: Replacing a String in a SELECT StatementIn 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:NameSalaryCityEmailNew EmailIDAnkit24500Delhi[email protected][email protected]Babita23600Noida[email protected][email protected]Chetan25600Noida[email protected][email protected]Deepak24300Delhi[email protected][email protected]Isha25900Delhi[email protected][email protected]Khushi24600Noida[email protected][email protected]Megha25500Noida[email protected][email protected]Parul23900Noida[email protected][email protected]Example 4: Replacing Strings in an UPDATE StatementTo 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:NameSalaryCityEmailAnkit24500Delhi[email protected]Babita23600Noida[email protected]Chetan25600Noida[email protected]Deepak24300Delhi[email protected]Isha25900Delhi[email protected]Khushi24600Noida[email protected]Megha25500Noida[email protected]Parul23900Noida[email protected]ConclusionModifying 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. Comment More infoAdvertise with us Next Article Replace String in SQL Server K khushboogoyal499 Follow Improve Article Tags : SQL Databases DBMS-SQL SQL-Server 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 Remove All Spaces From a String in SQL Server There are scenarios of the occurrence of spaces before and after a string and we may need to remove/trim the spaces for our use. Let us see how it is getting handled in SQL Server. Till SQL Server 2016, we have the functions called SQL LTRIM and SQL RTRIM functions. The name itself implies that LTRI 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 Reverse PIVOT Table in SQL Server In SQL Server, the PIVOT operation is a powerful feature that allows you to transform rows into columns, providing a convenient way to structure and analyze data. However, there are situations where we may need to reverse this operation, converting columns back into rows. This process is commonly kn 7 min read Insert Statement in MS SQL Server The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table.In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the help 4 min read Like