1

Is the following possible with just a sql query or do I need to create a php routine?

I have a database of members with gmail addresses, most of which contain 1 or more '.' (dots) in the address before the '@'

For the purposes of thwarting duplicate/multiple signups I am creating a new column that will contain the 'clean' gmail address so I can make comparisons going forward on new registrations.

Example... I have rows which the following gmail addresses are actually all the same in the eyes of gmail.

myname@gmail.com
m.yname@gmail.com
my.name@gmail.com
m.y.n.a.m.e@gmail.com

I know replace() can simply replace something with something, but I want to replace all dots before the @

Is there a sql function/statement I can use to clean up all the dots before the '@' or do I have to create a php routine to achieve this?

user1687533
  • 43
  • 2
  • 9
  • So what did you try already? – Rowland Shaw Nov 20 '13 at 10:39
  • Just out of curiosity.. but my.name@gmail.com is definatly another email address then myname@gmail.com. Wouldn't "fixing" these email adresses actually break the data in your database? – Damien Overeem Nov 20 '13 at 10:39
  • Maybe this will help http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql – fracz Nov 20 '13 at 10:40
  • @DamienOvereem. For gmail, those are the same. that's what he is trying to say. sending one email to those 4 mails will always send it to the same unique email address of myname@gmail.com – Filipe Silva Nov 20 '13 at 10:41
  • @FilipeSilva are you sure , all are same for gmail... – Kuldeep Choudhary Nov 20 '13 at 10:44
  • http://sqlfiddle.com/#!2/d41d8/25512 – juergen d Nov 20 '13 at 10:46
  • @KuldeepChoudhary. yep. :) see [here](http://gmailblog.blogspot.pt/2008/03/2-hidden-ways-to-get-more-from-your.html) – Filipe Silva Nov 20 '13 at 10:46
  • 1
    @DamienOvereem No they are the same. Google created this for filtering options. Dots before the @ are irrelevant. All the emails I show above will all go to myname@gmail.com. Very similar a gmail usr can also add keywords with a '+'. Example, myname+list1@gmail.com will go to myname@gmail.com. – user1687533 Nov 20 '13 at 10:47
  • Interesting, didn't know google handled email adresses that way. – Damien Overeem Nov 20 '13 at 11:19
  • Presumably, you'll want to ignore any portions after any + as well? – Rowland Shaw Nov 20 '13 at 12:08
  • @RowlandShaw That is already taken care of at registration by not allowing '+' in an email address. – user1687533 Nov 20 '13 at 14:58
  • @user1687533 In that case, I can't ever be your customer, as I have a `+` in my real email address... – Rowland Shaw Nov 20 '13 at 15:14
  • @RowlandShaw It is only the case of gmail, googlemail, and hotmail/live domains. MS added the use of keywords with plus sign as well. Real addresses where the plus sign is actually part of the real address are not affected. – user1687533 Nov 20 '13 at 19:09

6 Answers6

1

Try this

SELECT CONCAT(REPLACE(LEFT(email,LOCATE('@',email)-1), '.', ''),RIGHT(email, LOCATE('@',email))) FROM your_table.
clops
  • 5,085
  • 6
  • 39
  • 53
  • The basic algorithm here is as follows: split the address on the '@' sign, replace all dots in the left part, and concat the parts back together. – clops Nov 20 '13 at 10:46
1

Try this:

select concat(replace(substring(email, 1, locate('@', email)-1), '.', '') , substring(email, locate('@', email), length(email)))
vkamayiannis
  • 745
  • 3
  • 13
0

You can use a mix of CONCAT, REPLACE, SUBSTRING and LOCATE:

SELECT
    `email`,
    CONCAT(
        REPLACE(
            SUBSTRING(
                `email`,
                1, 
                LOCATE(
                    '@', 
                    `email`
                ) - 1
            ),
            '.', 
            ''
        ),
        '@',
        SUBSTRING(
            `email`,
            LOCATE(
                '@', 
                `email`
            ) + 1
        )
    ) as `email_clean`
FROM
    `emails`

SQLFiddle

┌──────────────────────┬─────────────────────┐
│        EMAIL         │     EMAIL_CLEAN     │
├──────────────────────┼─────────────────────┤
│ bar@foo.bar.com      │ bar@foo.bar.com     │
│ foo-bar@bar-foo.com  │ foo-bar@bar-foo.com │
│ foo.b.ar@bar.foo.com │ foobar@bar.foo.com  │
│ foo.bar@bar.foo.com  │ foobar@bar.foo.com  │
│ foo@bar.com          │ foo@bar.com         │
└──────────────────────┴─────────────────────┘
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

You can identify the section before the @, replace the dots, and then combine it with everything after the @:

SET @EMAIL='my.name@gmail.com';
SELECT CONCAT(REPLACE(LEFT(@EMAIL,INSTR(@EMAIL,'@')),'.',''),RIGHT(@EMAIL,CHAR_LENGTH(@EMAIL)-INSTR(@EMAIL,'@')))

The LEFT and RIGHT are used to split the string, the INSTR used to determine where to split the string, and the CONCAT chucks it all back together again. These sorts of things can be built up a step at a time - I started with finding my @, then getting the stuff to the LHS and so on...

Steph Locke
  • 5,951
  • 4
  • 39
  • 77
0
UPDATE some_table SET some_field = REPLACE(some_field, '&lt;', '<')

Please see the Ref URL : How can I use mySQL replace() to replace strings in multiple records?

hope this is useful for all of us.

At a very generic level

UPDATE MyTable

SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')

WHERE SomeOtherColumn LIKE '%PATTERN%'

In your case you say these were escaped but since you don't specify how they were escaped, let's say they were escaped to GREATERTHAN

UPDATE MyTable

SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')

WHERE articleItemLIKE '%GREATERTHAN%'

Since your query is actually going to be working inside the string, your WHERE clause doing its pattern matching is unlikely to improve any performance - it is actually going to generate more work for the server. Unless you have another WHERE clause member that is going to make this query perform better, you can simply do an update like this:

UPDATE MyTable

SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')

Coming in from a completely different angle:

You can do this when you select the data (not when you do save it)

So instead of :

SELECT MyURLString From MyTable

You can do

SELECT REPLACE (MyURLString, 'GREATERTHAN', '<') as MyURLString From MyTable
Community
  • 1
  • 1
Siraj Khan
  • 2,328
  • 17
  • 18
0

Here's a query which strips the first dots from the "name"-part in the mail adress

SET @mailAddress := 'm.y.n.a.m.e@gmail.com';
SELECT @mailAddress, 
    REPLACE(SUBSTRING_INDEX(@mailAddress, '@', 1), '.', '') namePart,
    SUBSTRING_INDEX(@mailAddress, '@', -1) domainPart,
    CONCAT(REPLACE(
        SUBSTRING_INDEX(@mailAddress, '@', 1)
    , '.', ''),
    '@',
    SUBSTRING_INDEX(@mailAddress, '@', -1)
) email;

Result:

@mailAddress            namePart  domainPart  email
--------------------------------------------------------------
m.y.n.a.m.e@gmail.com   myname    gmail.com   myname@gmail.com

SQLFiddle here: http://sqlfiddle.com/#!2/d41d8/25514

NoLifeKing
  • 1,909
  • 13
  • 27