0

I have a table of email addresses:

CREATE TABLE contacts(
    email     VARCHAR(255)
)

INSERT INTO contacts VALUES    
    ('[email protected]'),
    ('[email protected]'),
    ('[email protected]');

How can I find and replace the email format so [email protected] -> [email protected]?

E.g:

UPDATE contacts
SET email = REGEXP_REPLACE(email, '@', '@test.com');

Results in [email protected]

Playground here: https://dbfiddle.uk/GnIfomiO

2 Answers 2

1

This is probably most simply done by splitting the email address in two on the @, keeping the part before it and replacing . in the part after it with nothing. Then you can just append @test.com to the result:

UPDATE contacts
SET email = SPLIT_PART(email, '@', 1) || '_' || REPLACE(SPLIT_PART(email, '@', 2), '.', '') || '@test.com';

Output for your demo:

email
[email protected]
[email protected]
[email protected]

Demo on dbfiddle

Sign up to request clarification or add additional context in comments.

Comments

1

demo: https://dbfiddle.uk/0KWPVeAI

UPDATE contacts
SET email = REGEXP_REPLACE(email, '@', '_gmailcom@');

UPDATE contacts
SET email = REGEXP_REPLACE(email, '@.*$', '@test.com');

The regex pattern is @ follow all the chars to end of string

1 Comment

What if the email address is not @gmail.com?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.