Table Email
:
Values:
[email protected]
[email protected]
[email protected]
I want to replace the string before @
with test
.
Result:
[email protected]
[email protected]
[email protected]
How do I use substringing and replace based on a character in the string?
declare @t table(email varchar(30))
insert @t values('[email protected]'),
('[email protected]'),
('[email protected]')
select stuff(email, 1, charindex('@', email), 'Test@')
from @t
Result:
[email protected]
[email protected]
[email protected]
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 = '[email protected]'
PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email)))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With