Skip to content
Advertisement

Remove special symbols from email string

I need to replace some characters in email string, exactly such actions:

lower_email = str.lower(str.split(email,'@')[0])
nopunc_email = re.sub('[!@#$%^&*()-=+.,]', ' ', lower_email)
nonum_email = re.sub(r'[0-9]+', '', nopunc_email).strip()

But in SQL

I tried to use expression TRANSLATE(lower(email), 'a1_a.a-a@1-+()a ', 'a a a a'), but it didn’t give me solution.

Thanks in advance!

For example:

import re
email = 'some_email.example-2021@gmail.com'
lower_email = str.lower(str.split(email,'@')[0])
nopunc_email = re.sub('[!@_#$%^&*()-=+.,]', ' ', lower_email)
nonum_email = re.sub(r'[0-9]+', '', nopunc_email).strip()

result 'some email example'

Advertisement

Answer

SELECT email,
       TRIM(
         TRANSLATE(
           LOWER(SUBSTR(email, 1, INSTR(email, '@') - 1)),
           '!_#$%^&*()-=+.,0123456789',
           '               '
         )
       ) AS translated
FROM   table_name

Which, for the sample data:

CREATE TABLE table_name (email) AS
SELECT 'some_email.example-2021@gmail.com' FROM DUAL;

Outputs:

EMAIL TRANSLATED
some_email.example-2021@gmail.com some email example

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement