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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Changing the domain names in email id to test.com

Author  Topic 

Blueshadow
Starting Member

7 Posts

Posted - 2010-08-12 : 05:46:45
Hi, I have an Employee table with 3 columns as EmpID,EmpName and EmailID in the following fomat

EmpID EmpName EmailID
10 Jason jason@abc.com
11 Robynne robin@bnm.com
12 Philip philip@vbn.com
13 Sam sam@dsf.com
14 Tim tim@dsf.com
15 Ken ken@dsf.com

I need to change the domain names to test.com
as shown below:
10 Jason jason@test.com
11 Robynne robin@test.com
12 Philip philip@test.com
13 Sam sam@test.com
14 Tim tim@test.com
15 Ken ken@test.com

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-08-12 : 06:03:11
declare @email varchar(255)
set @email='jason@abc.com'

select
left(@email,charindex('@',@email))+'test.com'



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-12 : 06:06:18
You should be using example.com, not test.com (which is a valid domain and will be deluged with emails if any of those addresses actually gets emailed!)

You can use something like STUFF(MyColumn, CHARINDEX('@', MyColumn), nnn, '@example.com')

I prefer webfred's use of LEFT()
Go to Top of Page

Blueshadow
Starting Member

7 Posts

Posted - 2010-08-13 : 05:42:54
Hi, using the following query I can only select the column values with updated email ids like
Jason@test.com.

DECLARE curEmployee CURSOR
FOR SELECT Emailid FROM tbEmp
DECLARE @email VarChar(100)
OPEN curEmployee
FETCH NEXT FROM curEmployee INTO @Email
WHILE @@Fetch_Status = 0
BEGIN
select
left(@email,charindex('@',@email))+'test.com'
FETCH NEXT FROM curEmployee INTO @email
END
CLOSE curEmployee

But my requirement is to update the table with the email ids like jason@test.com.
I am not getting the format as to how to use the functions left,charindex and the cursor with the Update clause, so that all the domain names will be changed to test.com or example.com.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-13 : 07:30:19
Don't need a cursor do you?

UPDATE U
SET Emailid = left(@email,charindex('@', Emailid))+'example.com'
FROM tbEmp
WHERE Emailid LIKE '%@%'
Go to Top of Page

Blueshadow
Starting Member

7 Posts

Posted - 2010-08-13 : 07:40:24
Kristen,
How does all emailid's from the table come to the variable @emailid?
When I am execting the query you typed I am getting the message as "Must declare the scalar variable @email"
When I am declaring the variable @email and executing the whole query as:
declare @email varchar(255)
UPDATE tbemp
SET Emailid = left(@email,charindex('@', Emailid))+'example.com'
FROM tbEmp
WHERE Emailid LIKE '%@%'
I am getting all email id columns as NULL
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-08-13 : 07:53:42
quote:
Originally posted by Blueshadow

Kristen,
How does all emailid's from the table come to the variable @emailid?
When I am execting the query you typed I am getting the message as "Must declare the scalar variable @email"
When I am declaring the variable @email and executing the whole query as:
declare @email varchar(255)
UPDATE tbemp
SET YourEmailcolumn = left(YourEmailcolumn,charindex('@', YourEmailcolumn))+'example.com'
FROM tbEmp
WHERE YourEmailcolumn LIKE '%@%'





No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Blueshadow
Starting Member

7 Posts

Posted - 2010-08-13 : 08:19:47
I got it friends...
Thanks a ton..
Just two lines of code was enough
UPDATE tbemp
SET Emailid = left(Emailid,charindex('@', Emailid))+'example.com'
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-13 : 09:49:26
You will update columns that have no valid "@" in them. May be zero rows, but its still poor programming style. Every row updated goes into the LOG. If you update rows with no change you are logging them unnecessarily - e,.g. if column contains NULL

Also, if YourEmailcolumn does not contain "@" the LEFT() and concatenation of 'example.com' is going to make a mess of the data.
Go to Top of Page
   

- Advertisement -