Solved

Make duplicate codes unique

Posted on 2013-12-07
2
261 Views
Last Modified: 2013-12-09
Hi Experts,

  Please do refer the attached document. Some codes are duplicate here.

I look for update query to make the duplicates as unique.

Say for example:

If the codes are duplicate as: QAIN0001, QAIN0001, QAIN0001
and the expected update as QAIN0001, QAIN0002, QAIN0003

Please help.
DupCode.xlsx
0
Comment
Question by:Easwaran Paramasivam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 39702918
you can use row_number function to update your records

;with cte as (
  select row_number() over (order by updatedfield) rn, *
  from yourtable
)
update cte
set updatedfield = 'QAIN000' + cast(rn as varchar)
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 39705591
Assuming your Code values are 8 characters and the last 4 are digits and you will always start with 1 when reassigning the new values then this worked on a test table I created with your sample data.  It only updated the ones that were complete duplicates and left the others alone.
;with cte as (
  select row_number() over (partition by substring(Code,1,4) order by ID) rn, count(*) over (partition by Code) cnt, *
  from BadCodes
)
update cte
set Code= substring(Code,1,4) + right('000' + cast(rn as varchar),4)
WHERE cnt > 1;
 

Open in new window

0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question