Wednesday, 18 February 2015

Delete duplicate rows in SQL Server

Introduction

Some time some duplicate data may inserted in the SQL table. But its harmful for you and your application too. So how to remove the duplicate rows in your table.

Description

Take a look in the following table content. Its having duplicate rows.


Row 1,2,3 and 4 are duplicate. So we have to remove one row and keep one in the table.
Now question is how to do?

To solve this problem take a look at the following code. This will solve this problem.

  WITH tblTemp as
(
SELECT ROW_NUMBER() Over(PARTITION BY Name,Department ORDER BY Name)
   As RowNumber,* FROM emp
)
DELETE FROM tblTemp where RowNumber >1

After run these code show the table data, it will all unique rows present.

Related Posts:

  • UDF For Splitting Comma Seperated Values CREATE FUNCTION [dbo].[fnSplit](        @sInputList VARCHAR(8000) -- List of delimited items      , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items&nbs… Read More
  • Different Types of SQL Joins S ql joins are used to fetch/retrieve data from two or more data tables, based on a join condition. A join condition is a relationship among some columns in the data tables that take part in Sql join. Basi… Read More
  • Delete duplicate rows in SQL Server Introduction Some time some duplicate data may inserted in the SQL table. But its harmful for you and your application too. So how to remove the duplicate rows in your table. Description Take a look in the following tabl… Read More
  • Schema in SQL Server 2012 IntroductionIn this article I describe schemas in SQL Server 2012. A schema is like a container that contains database objects like tables, views etc. So let us learn about schemas, creation of schemas, how to alter schemas … Read More
  • Built-in Functions (Transact-SQL) CHARINDEX - Searches an expression for another expression and returns its starting position if found. DECLARE @FullName VARCHAR(100) SET @FullName = 'Clark Kent' select CHARINDEX(' ', @FullName) Output – 6 STUFF… Read More

0 comments:

Post a Comment