How to replace string in SQL Server?
Introduction:
--------------------
Here i will give an example how to replace a substring in a string in SQL Server.
For example you want to change domain in Employee email addresses in 'EmployeeDetails' table.
In the below example i am replacing EmailId from "@gmail.com" to "studentboxoffice.in" in 'EmployeeDetails' Table .
Example: 1)
-------------------
Declare @txt1 varchar(50)
set @txt1 = 'vikramreddy@gmail.com'
/* Here i am replacing @gmail.com to @studentboxoffice.in */
set @txt1 = REPLACE(@txt1, '@gmail.com', '@studentboxoffice.in')
select @txt1
Output:
--------------
vikramreddy@studentboxoffice.in
Exmple: 2)
----------------
You can also update employee emailid in 'EmployeeDetails' table.
-- before update EmailID = 'vikramreddy@gmail.com'
update EmployeeDetals
set EmailID = REPLACE(EmailID, '@gmail.com', '@studentboxoffice.in')
where EmployeeCode = 3151
-- After updating EmailID = 'vikramreddy@studentboxoffice.in'
-
CreatedAug 20, 2013
-
UpdatedOct 03, 2020
-
Views1,957
Related Articles
Different types of variables in SQL Server
What is Cross Join in SQL Server
How delete record from parent table when there is Foreign Key relation in SQL Server
What is the differences between CHAR, NCHAR, VARCHAR and NVARCHAR in SQL Server?
How to add or remove a column from a table in SQL Server?
How to insert values into Identity Column in SQL Server?
How to get a fixed-length value in SQL Server?
What is Left Join (or) Left Outer Join in SQL Server?
How to get current week dates in SQL Server?
What are the different types of Date formats in SQL Server?