How to replace a substring of a string before a specific character?

Email table:

values:

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

I want to replace the line before @ with test .

Result:

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

How to use substring and substitution based on character in string?

+6
source share
4 answers

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 check it as follows:

 DECLARE @email nvarchar(50) SET @email = ' carmine32@hotmail.com ' PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email))) 
+5
source
 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 
+3
source

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

+2
source

You could

 select 'test' + substring(fld, charindex('@', fld), len(fld)) 
+1
source

Source: https://habr.com/ru/post/925134/


All Articles