Add repeat data (A) back from the number of growth (a-1,a-2 ... )

Source: Internet
Author: User

database table Test

ID Auto-Growing primary key

Name nvarchar (10)

ID Name

1 A

2 A

3 A

4 B

5 B

6 C

7 C

Add -1,-2 after the duplicate name ....

As shown below

ID Name

1 A

2 A-1

3 A-2

4 B

5 B-1

6 C

7 C-1


Select id ,name = < Span style= "color: #808080;" >name + when no = 1 then else "-' rtrim ( - 1) end into #AA

From (Select *, no = row_number () over ( Partition by name order id ) from test ) t

Order by ID

Update test set name= (select name from #AA where id=test.id)


Add repeat data (A) back from the number of growth (a-1,a-2 ... )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.