Thursday, 15 August 2013

How to Replace substring in a string.

 

Here i will give an example how to replace a substring in a string. For example you want to change email address in employee details table. 

In below example i am replacing  EmailId  from “@gmail.com” to ‘”@yahoo.com” in Employee Table .

-- Exmple 01.

declare @txt1 varchar(50)
set @txt1 = 'srinu.munagala33@gmail.com'
-- Here iam replacing @gmail.com to @yahoo.com
set @txt1 = REPLACE(@txt1, '@gmail.com', '@yahoo.com')

select @txt1
-- out put
-- srinu.munagala33@yahoo.com
-- Exmple 2.
/*
You can also update employee emailid in employee table.
*/

-- before EmailID = srinu.munagala33@gmail.com

update EmployeeDetals
set EmailID = REPLACE(EmailID, '@gmail.com', '@yahoo.com')
where EmployeeCode = 3151

-- After updating EmailID = srinu.munagala33@yahoo.com

No comments:

Post a Comment