[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Make duplicate codes unique

Posted on 2013-12-07
2
Medium Priority
?
264 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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