Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Remove (Delete) Duplicate Records from Table using Query

Apr 22, 2015
Introduction:

Here will explain how to remove duplicate records or rows from a table in SQL Server or delete duplicate records from table in SQL Server.

Description:

In previous articles I explained take database backup in sql server, covert rows to columns in sql server without using pivot tablesubstring function in SQL serverReplace function in SQL Serverhow to delete duplicate records from datatable in asp.net. Now I will explain how to delete duplicate records from a datatable in SQL server. 

During work with one application I got requirement like get the unique records from datatable in sql server. Actually our datatable does not contain any primary key column because of that it contains duplicate records that would be like this

Actually above table does not contain any primary key column because of that same type of records exist.

Now I want to get duplicate records from datatable for that we need to write query like this

WITH tempTable as
(
SELECT ROW_NUMBER() Over(PARTITION BY Name,Position ORDER BY Name) As RowNumber,* FROM EmployeData
)
SELECT * FROM tempTable

Once we run above query we will get data like this

If you observe above table I added another column RowNumber this column is used to know which record contains duplicate values based on rows with RowNumber greater than 1.  

Now we want to get the records which contains unique value from datatable for that we need to write the query like this


WITH tempTable as
(
SELECT ROW_NUMBER() Over(PARTITION BY Name,Position ORDER BY Name) As RowNumber,* FROM EmployeData
)
DELETE FROM tempTable where RowNumber >1
SELECT * FROM EmployeData order by Id asc

Once we run above query all duplicate records will delete from our table and that would be like this

SQL Server Remove (Delete) Duplicate Records from Table using Query

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Parameswarn R said...

I have doubt , after execute the Query where is the RowNumber Column?? ,Previous table you marked by red color to the column of RowNumber after execute it, It was hided how that was Happen???

Nithin Ramakrishnan said...

tempTable is the temperory result set created using CTE in sql which has Rownumber in the selection but EmployeData is the actual table in database. Hope it helps.

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.