I’m trying to get the primary email from aliases, for example, I have the following examples and I need to normalize them.
- from “test.me@email.com” to “testme@email.com”
- from “test+me11@email.com” to “test@email.com”
- from “test.me.123+22@email.com” to “testme123@email.com”
- from “test+123@email.com” to “test@email.com”
I wrote a SQL statement on bigquery but got an error that says “Third argument in SUBSTR() cannot be negative”
SELECT TRANSLATE(SUBSTR('test+123@email.com', 1, INSTR('test+123@email.com', '+')-1),'+.', '') ||'@' || SUBSTR('test+123@email.com', Instr('test+123@email.com','@')+1)
Any ideas or suggestions for the above or ideas for useing regex_replace instead
Thanks
Advertisement
Answer
Consider below approach
select email, regexp_replace(email, r'([^+@]+)+?([^@]*(.*))', r'13') original_email from your_table
if applied to sample data in your question – output is
In case if for some reason you want dots to be removed – use below
select email, original_email, format('%s@%s', replace(split(original_email, '@')[offset(0)], '.', ''), split(original_email, '@')[offset(1)] ) with_stripped_dots from your_table, unnest([regexp_replace(email, r'([^+@]+)+?([^@]*(.*))', r'13')]) original_email
with output