Search code examples
sqlsql-servertrimsql-server-2017

How to trim emails in a column and at the same time remove duplicates (MSQL2017, UPDATE column)


I have a table with some data one of them are Emails that are stored in column called "Domain".

I would need to update "Domain" so I cut the emails and only leave domains (Example: [email protected] that would after query be like this testmail.com).

So the Domain column has over 290000 emails stored inside and there are also some NULL's...
As I said I am looking for domains and I don't need data left of and including @
Also don't need duplicated domain list or NULL values in Domain column.

So what I am looking for is a query that will trim the emails to transform them into domains at the same time the query would remove the data with NULL value and duplicated domains.

The final result should be column Domain with only unique domains and without NULLs inside.


Solution

  • As simple as:

    SELECT DISTINCT RIGHT(Email, LEN(Email) - CHARINDEX('@', email)) AS email_domain
    FROM table
    WHERE Email IS NOT NULL;