Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 duplicate update

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-03-28 : 23:33:02
i have table applicant_form with two column ( id numeric(10) pk, formno varchar(50) )
i need to create bunique constartint on formno. but user g=had entered
some duplicate values in that,

i need to replace first duplicate with dupli_1_+ form_no.second with dupli_2_form_no
and so on. please suggest.


challenge everything

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-28 : 23:45:15
[code]
update t
set formno = 'dupli_' + convert(varchar(10), rn - 1) + '_' + formno
from
(
select id, formno, rn = row_number() over (partition by formno order by id)
from applicant_form
) t
where t.rn > 1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -