SQL Server: How to replace string in SQL Server

 SQL Server     sbo     Updated on Aug 20, 2013     VIKRAM     579   

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'

  Related Articles
SQL Server: Variables
SQL Server: Differences between CHAR, NCHAR, VARCHAR and NVARCHAR
SQL Server: System Databases
SQL Server: Insert values into Identity Column
ASP.Net: List View Basic Example
JQuery: How to replace string in JQuery
Java Script: How to replace string in Java Script
C#: How to replace string in C#
C#: How to convert string to int in C#
SQL Server: How to get fixed length number format in SQL Server (or) How to prefix zeros to number, to get fixed length number format in SQL Server
  Recent Articles
SQL Server: Variables
SQL Server: Differences between CHAR, NCHAR, VARCHAR and NVARCHAR
SQL Server: System Databases
SQL Server: Insert values into Identity Column
ASP.Net: List View Basic Example
JQuery: How to replace string in JQuery
Java Script: How to replace string in Java Script
C#: How to replace string in C#
C#: How to convert string to int in C#
SQL Server: How to get fixed length number format in SQL Server (or) How to prefix zeros to number, to get fixed length number format in SQL Server