6

Email

价值观:

josh@yahoo.com
carmine32@hotmail.com
zehmaneh@yahoo.com

我想@test.

结果:

test@yahoo.com
test@hotmail.com
test@yahoo.com

如何根据字符串中的字符使用子字符串和替换?

4

4 回答 4

7

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)))
于 2012-09-11T14:14:21.303 回答
3
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

结果:

Test@yahoo.com
Test@hotmail.com
Test@yahoo.com
于 2012-09-11T14:28:48.803 回答
2

您可以使用SUBSTRINGCHARINDEX

UPDATE Email set email = 
    'test' + SUBSTRING(email, CHARINDEX('@',email), LEN(email))

小提琴:http ://sqlfiddle.com/#!3/0face/6/0

于 2012-09-11T14:17:18.620 回答
1

你可以

select 'test' + substring(fld, charindex('@', fld), len(fld))
于 2012-09-11T14:15:56.527 回答