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.

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

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

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     │
├──────────────────────┼─────────────────────┤
│ [email protected][email protected]     │
│ [email protected][email protected] │
│ [email protected][email protected]  │
│ [email protected][email protected]  │
│ [email protected][email protected]         │
└──────────────────────┴─────────────────────┘
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='[email protected]';
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 := '[email protected]';
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
--------------------------------------------------------------
[email protected]   myname    gmail.com   [email protected]

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

NoLifeKing
  • 1,909
  • 13
  • 27