Posted By

aubreyWlove on 09/01/18


Tagged

sql server 2012


Versions (?)

Find duplicates in SQL Server


 / Published in: SQL
 

This will find any duplicates in a table and assign them a repeating number. To delete the duplicates simply change the "SELECT * " with "DELETE". You will need to change the table from "demo1" to your table name and the column "empid" to your desired column name. Don't forget to remove the equal sign in the "FROM DupCheck" line or it will delete all entries.

  1. WITH DupCheck
  2. AS
  3. (
  4. SELECT *,
  5. ROW_NUMBER() OVER(PARTITION BY empid ORDER BY empid) AS 'ROW #'
  6. FROM demo1
  7. )
  8. SELECT *
  9. FROM DupCheck WHERE [ROW #] >= 1;
  10. GO

Report this snippet  

You need to login to post a comment.