Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
gyana.ojha
Starting Member
5 Posts |
Posted - 2010-07-19 : 09:21:50
|
Hello All Forum Member.. I Need a help ...My requirement is given belowI have a Email Field In my table ....Suppose I have xyz@gmail.com, abc@yahoo.com and 123@hotmail.comSo My question is How Can I updated only domain name is a single update query.For Example update xyz@gmail.com to xyz@AOL.com update abc@yahoo.com to abc@rediffmail.comThanks and RegardsGyana Ranjan Ojha |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-07-19 : 09:26:48
|
replace(Your_Column,'@gmail.','@AOL.') No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gyana.ojha
Starting Member
5 Posts |
Posted - 2010-07-19 : 09:53:47
|
Hope You didnt get my queries. Above statement only update one record I want update all record by replace domain nameThanks and RegardsGyana Ranjan Ojha |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-07-19 : 13:00:19
|
This requirement can be completed in multiple ways. I will explain only two ways.1. Using multiple case statement.Declare @S table(OldEmail varchar(500), NewEmail varchar(500))Insert into @S (OldEmail)Select 'AAA@yahoo.com' unionSelect 'AAA@hotmail.com' unionSelect 'bbA@hotmail.com' unionSelect 'bbA@Mymail.com' select * from @sUpdate @S set NewEmail =Case When CHARINDEX('@yahoo.', OldEmail)>0 Then replace(OldEmail,'@yahoo.','@AOL.')When CHARINDEX('@hotmail.', OldEmail)>0 Then replace(OldEmail,'@hotmail.','@SQLMail.')When CHARINDEX('@Mymail.', OldEmail)>0 Then replace(OldEmail,'@Mymail.','@Gmail.')else oldEmailEndselect * from @sYou can use this method when the domain names to be replaced are few.2. Using a temporary table having old domain and new domain.--Data table having email valuesDeclare @S table(OldEmail varchar(500), NewEmail varchar(500))--Table having domain namesDeclare @Domain table(Old varchar(500), New varchar(500))Insert into @S (OldEmail)Select 'AAA@yahoo.com' unionSelect 'AAA@hotmail.com' unionSelect 'bbA@hotmail.com' unionSelect 'bbA@Mymail.com' Insert into @Domain (Old,New)Select '@yahoo.','@AOL.' unionSelect '@hotmail.','@SQLMail.' unionSelect '@Mymail.','@Gmail.' Update @S Set NewEmail = replace(OldEmail, d.Old, d.new)from @S s , @Domain dwhere s.OldEmail like '%'+d.Old+'%' Select * from @sRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
|
|
|
|