如何在特定字符之前替换字符串的子字符串?

RJ.*_*RJ. 6 sql t-sql replace substring

Email:

价值观:

josh@yahoo.com
carmine32@hotmail.com
zehmaneh@yahoo.com
Run Code Online (Sandbox Code Playgroud)

我想之前替换字符串@test.

结果:

test@yahoo.com
test@hotmail.com
test@yahoo.com
Run Code Online (Sandbox Code Playgroud)

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

Lit*_*les 6

你甚至不需要使用substring或者replace,你可以使用这个:

SELECT 'test' + RIGHT(email, charindex('@', REVERSE(email)))
FROM YourTable
Run Code Online (Sandbox Code Playgroud)

你可以用这个来测试它:

DECLARE @email nvarchar(50)
SET @email = 'carmine32@hotmail.com'
PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email)))
Run Code Online (Sandbox Code Playgroud)