Updating unique rows to become non-unique in SQL Server

I have a table with 3 columns containing values in rowid, documentNumver, customerNumber.

Col 2 contains mostly unique values although not always the case. The same is the case with col 3.

There are cases where for a particular documentNumber, there are a number of customerNumbers which are all unique.
In this cases, what I need to do is make the documentNumber unique for each customerNumber by changing the 3rd and 4th characters of the documentNumber to 01,02,03...

For eaxample,
rowid      Document     Customer
17738      2500000229      10106
17739      2500000229      10306
17740      2500000229      10146

So in the above case, I want the end result to be :
rowid      Document     Customer
17738      2501000229      10106
17739      2502000229      10306
17740      2503000229      10146

Your help will be highly appreciated.

R.
LVL 16
Richard OlutolaConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can update the things using the below query

;WITH CTE AS
(
SELECT * 
,ROW_NUMBER()OVER(PARTITION BY DOCUMENT ORDER BY ROWID) RN
FROM  [dbo].[ExpertTbl]
)

UPDATE CTE
SET Document = Document + (RN*1000000)
WHERE RN > 1

SELECT * FROM ExpertTbl

Open in new window

0
ZberteocCommented:
I'll use Viskas code with some modifications:
-- if document is an integer then:
;WITH CTE AS
(
SELECT * 
,ROW_NUMBER()OVER(PARTITION BY DOCUMENT ORDER BY ROWID) RN
FROM  [dbo].[ExpertTbl]
)

UPDATE CTE
SET Document = Document*100 + RN 

-- if document is varchar
;WITH CTE AS
(
SELECT * 
,ROW_NUMBER()OVER(PARTITION BY DOCUMENT ORDER BY ROWID) RN
FROM  [dbo].[ExpertTbl]
)

UPDATE CTE
SET Document = Document + right('00'+cast(RN as varchar(5)),2)

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
What is the datatype of DocumentNumber?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

awking00Commented:
Also, what happens if you have a record like the following with only one Customer for a given DocumentNumber?
rowid      Document     Customer
 17741      2500000230      10446
Should the DocumentNumber remain as is or should it be changed to 2501000230?
0
Richard OlutolaConsultantAuthor Commented:
awking00,
document number is currently varchar but I could change it if it makes life easier.

In response to your 2nd question, if there is only 1 cust / doc# then doc# remains the same.
There are also cases where there are more than 1 doc# and the cust is also the same. In this case also, the doc# is left as is.

For example:
rowid      Document     Customer
 17741      2500000230      10446
 17742      2500000230      10446
 17743      2500000230      10446

Hope this makes sense.

Thanks.
0
Richard OlutolaConsultantAuthor Commented:
awking00,
I am still awaiting your response if there is one, otherwise let me know so I can close this question.

Thanks.
0
awking00Commented:
Sorry, I haven't abandoned you, but I've been extremely tied up and not had a chance to test a solution. If you come up with something that [always] works (although I'm not convinced the solutions proposed to date work in every case), please feel free to go ahead and close the question. If not, I will try to find time to work on this. :-)
0
Richard OlutolaConsultantAuthor Commented:
Although I haven't had the chance to test this and the project focus has somewhat changed but I trust the solution provided.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.