Table Email
:
Values:
josh@yahoo.com
carmine32@hotmail.com
zehmaneh@yahoo.com
I want to replace the string before
declare @t table(email varchar(30))
insert @t values('josh@yahoo.com'),
('carmine32@hotmail.com'),
('zehmaneh@yahoo.com')
select stuff(email, 1, charindex('@', email), 'Test@')
from @t
Result:
Test@yahoo.com
Test@hotmail.com
Test@yahoo.com
You can use SUBSTRING and CHARINDEX:
UPDATE Email set email =
'test' + SUBSTRING(email, CHARINDEX('@',email), LEN(email))
Fiddle: http://sqlfiddle.com/#!3/0face/6/0
You don't even need to use substring
or replace
, you can use this:
SELECT 'test' + RIGHT(email, charindex('@', REVERSE(email)))
FROM YourTable
You can test it out with this:
DECLARE @email nvarchar(50)
SET @email = 'carmine32@hotmail.com'
PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email)))
You could
select 'test' + substring(fld, charindex('@', fld), len(fld))