SQL - loop and add string to end

select shiptoname  from customershipto

This return 10000 names.

How can I loop in customershipto table and change from customer name to customer name & "_number" at the end.

Sample Customershipto table return

Medical
Hopital
FieldMed

Change to

Medical_1
Hopital_2
FieldMed_3
etc...
VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ste5anSenior DeveloperCommented:
E.g.

SELECT	name + '_' + CAST(ROW_NUMBER() OVER ( ORDER BY name ) AS VARCHAR(255) )
FROM	sys.tables
ORDER BY name;

Open in new window

0

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
VBdotnet2005Author Commented:
I am so sorry. I want to do update instead of select. For every row on customer name, I want to update like my sample above.
0
VBdotnet2005Author Commented:
Customer_1
Customer_1
Customer_1
Etc...
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

ste5anSenior DeveloperCommented:
Then just update it..

 
DECLARE @Sample TABLE ( Payload NVARCHAR(255) );

INSERT INTO @Sample
VALUES	( 'A' ),
	( 'B' ),
	( 'C' );

WITH Data AS (
	SELECT	Payload,
		'_' + CAST(ROW_NUMBER() OVER ( ORDER BY Payload ) AS VARCHAR(255) ) AS Suffix
	FROM	@Sample
	)
	UPDATE	Data
	SET	Payload = Payload + Suffix;

SELECT	*
FROM	@Sample;

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
VBdotnet2005, do you still need help with this question?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Please, don't accept my last comment as an answer. You need to accept the comments that REALLY helped you to solve the issue.
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.