Farhad-Taran
Farhad-Taran

Reputation: 6522

is it possible to set column value to a concatenation of string and iteration value?

Is it possible to set value of a varchar column to a concatenation of a string and the result set iteration.

for example:

update TB_USER set LOGIN_NAME = 'BOB'+index where LOGIN_NAME = 'BOB'

results in the following:

LOGIN_NAME

BOB0

BOB1

BOB2

Upvotes: 1

Views: 88

Answers (1)

gbn
gbn

Reputation: 432561

update T
SET LOGIN_NAME = LOGIN_NAME + CAST(rn AS varchar(10))
FROM
   (SELECT
       LOGIN_NAME,
       ROW_NUMBER() OVER (ORDER BY something /*or (SELECT 1)/*) as rn
    FROM
       TB_USER
    WHERE
       LOGIN_NAME = 'Bob'
    ) T

Upvotes: 5

Related Questions