Thursday, August 13, 2009

Duplicate Record in SQL

How do you find duplicate records in a table in SQL database, here is a script

Select First_Name, Last_Name, Email, Phone, count(*)
from Table_ABC
having count(*) > 1
group by First_Name, Last_Name, Email, Phone


How to update record in a table based on values in another table

Update Table_ABC
set First_Name = (select FIRSTNAME from Table_BCD where Table_ABC.Email = Table_BCD.Email)
where exists (select FIRSTNAME from Table_BCD where Table_ABC.Email = Table_BCD.Email)


If the table_BCD have duplicate emails, you can pick the first one by using the following script.

Update Table_ABC
set First_Name = (select top(1) FIRSTNAME from Table_BCD where Table_ABC.Email = Table_BCD.Email)
where exists (select top(1) FIRSTNAME from Table_BCD where Table_ABC.Email = Table_BCD.Email)


I found these scripts are very useful.

No comments:

Post a Comment