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.
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 about substring function in SQL server
and Replace function in SQL Server and I explained how 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
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. |
|||
|
|||
17 comments :
hi MR.suresh i want to know About Insteller class can u please give the demo on that u r site is so good and usefull please continue posting thank u
hi MR.suresh i want to know About Insteller class can u please give the demo on that u r site is so good and usefull please continue posting thank u
Hi Suresh.. when i execute this query in sql server it gives me an error :- "Invalid column name 'RowNumber'."
Can you Please help...
Here i taken RowNumber as a just columnname to display result of count i hope you made it as separate column with datatable. please check the query what i written...
I just created a table like u had. Then i ran the above mentiones query in query browser in SQL Server. The output i got is Invalid column name 'RowNumber. Can you explain this. I havent taken any datatable. i just executed the query
Got it....!!!!!
You are awesome - Very very helpful
Recently I had a question in interview as follows
For example my name is Dorababu some of them inserted as Dorababu,Dhorababu and dorebabu like this. I would like to delete the remaining except Dorababu from the table how can I do this
Dear Suresh, Is that only way to find duplicate record and delete them and is that Optimized query?
I don't know how to remove my date to SQL server?
AWESOME!!!!! Thanks a lot.
Respected Sir,
can u tell me the best books for asp.net and sqlserver2012.
Every time i read your blocks sir they are really usefull for me .........thanks a lot boss
sir can u pls tel me how to use WITH tempTable as() function in c# code
It's very useful for me ...........Thank u suresh
we can use like this - delete emp-table where empid
in (select empid from emp-table group by empid having count(*)>1)
I regular Visit Your Website And I Take Lot Of Use Things Here And Then Use It Own Website.
First Of All I Give Thanks To Your Efforts Regarding Your Help For New Comer In This Field.
Note: Only a member of this blog may post a comment.