Solved

Make duplicate codes unique

Posted on 2013-12-07
2
258 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
2 Comments
 
LVL 32

Expert Comment

by:ewangoya
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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now