2

I want to replace all the dots before @ in an email with empty string in oracle query

like:

anurag.mart@hotmail.com >> anuragmart@hotmail.com
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
  • Welcome to Stack Overflow. Take a [**tour**](http://stackoverflow.com/tour) to understand how this site works! – Lalit Kumar B Nov 28 '15 at 13:36
  • 2
    I'm not sure why you'd want to do this, but be warned that [only Google ignores periods in the local part of an email address](http://stackoverflow.com/questions/14865869/do-all-email-providers-ignore-periods-in-front-of). Hotmail probably treats `anurag.mart@hotmail.com` and `anuragmart@hotmail.com` as different addresses, and per the RFC they are. – Bacon Bits Nov 28 '15 at 14:29

3 Answers3

3

The easiest way is to use REGEXP_REPLACE to identify the pattern and replace it with required pattern.

regexp_replace('anurag.mart@hotmail.com', '(\w+)\.(\w+)(@+)', '\1\2\3')

For example,

SQL> SELECT 'anurag.mart@hotmail.com' email_id,
  2  regexp_replace('anurag.mart@hotmail.com', '(\w+)\.(\w+)(@+)', '\1\2\3') new_email_id
  3  FROM dual;

EMAIL_ID                NEW_EMAIL_ID
----------------------- ----------------------
anurag.mart@hotmail.com anuragmart@hotmail.com
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
3
  • Instr - To identify the position(@)
  • Substr - To extract data between start(1) and end(@) position
  • Replace - To replace . with ''
  • || - To concatenate two strings

Try this

SELECT Replace(Substr('anurag.mart@hotmail.com', 1, 
                      Instr('anurag.mart@hotmail.com', '@', 1)), '.', '') 
       || Substr('anurag.mart@hotmail.com', Instr('anurag.mart@hotmail.com','@')+1) 
FROM   dual 

Result:

anuragmart@hotmail.com

SqlFiddle Demo

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
0

I came on this page while looking for solutions for SQL servers, I converted the above for SQL server for my project, Here is SQL if anybody else needs it.

SELECT 
    CONCAT(
        REPLACE(
            SUBSTRING(EmailAddress, 1, CHARINDEX('@', EmailAddress)-1),
            '.',
            ''
        ), 
        SUBSTRING(EmailAddress, CHARINDEX('@', EmailAddress), LEN(EmailAddress)) 
    )
FROM [Applicant]
Developer
  • 25,073
  • 20
  • 81
  • 128