Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Duplicate Records

Status
Not open for further replies.

rarubio1

Programmer
Jan 25, 2002
64
US
What is the SQL syntax for deleting duplicate records from a table in SQL Server.

Thanks
RR
 
Obviously it depends on your columns, what you consider to be a duplicate and how you decide which of them to keep.

eg, assume you have a table of email addresses which have subscribed to your mailing list. The table contains fields for email address and date it subscribed. You want to delete any duplicate addresses, leaving the earliest date. One way to do this is below:

Code:
CREATE TABLE Emails (
  Email varchar(50),
  SubsDate datetime
)

DELETE emails
FROM emails e
WHERE subsdate <> (
  SELECT MIN(subsdate)
  FROM emails
  WHERE email = e.email
)

--James
 
I usually do the following:

SELECT DISTINCT * INTO #temp FROM Table

DELETE FROM Table

INSERT INTO Table() SELECT * FROM #temp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top