1

I'm after some help putting together a query to remove duplicate data in a specific field in one of my tables.

I have a table called contacts that I need to import in to another system. The new system requires that the email field be unique. I need a query that will allow me to search the email field and remove any duplicate data or set it to "".

I don't want to remove the rows, just the duplicate of the email. So if there are two records contain the email [email protected], then I want to keep the first reference while removing the second.

Seems like this should be a simple thing to do but I'm struggling working our how to achieve it. Thanks.

1
  • Do you have a primary key on the table? Commented Aug 6, 2013 at 17:55

2 Answers 2

3

You need to use a query similar to the following:

UPDATE CONTACTS A, CONTACTS B
  SET B.EMAIL=NULL
WHERE A.EMAIL=B.EMAIL
  AND A.KEY_FIELD>B.KEY_FIELD

Use the field reference to determine which is removed.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks. What's the A and B bit for? I'm pretty green with SQL.
The A and B are aliases for the table - it allows you to join a table with itself and thus look for the duplicates.
Oh great, and finally what is the KEY_FIELD part?
There must be another column in your contacts table that determines which is the first email reference (for example, a row ID or maybe a timestamp for the insertion). KEY_FIELD is my place holder for this column - you need to use whatever the suitable column in your table is.
well I have a unique id column and I tried with that but it said it didn't exist?
|
0
UPDATE CONTACTS SET b.email = ""
FROM CONTACTS a, CONTACTS b
WHERE a.EMAIL = b.EMAIL AND a.ID>b.ID

Another Possible solution

Reference :

http://stackoverflow.com/questions/2044467/how-to-update-two-tables-in-one-statement-in-sql-server-2005

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.