Reputation: 21
I have a string in the field context
. The strings in this field are build in two versions. I need to append the string by following logic.
“
in this string“
with following string: 123Now I tried this without success
declare @pf varchar(10), @pos int,
select @pflist = context,
@pos = LEN(@pflist) - CHARINDEX('"',REVERSE(@pflist))
UPDATE table SET CONTEXT = str_replace(convert(varchar(255),CONTEXT), ',
"'+ @pos +'"', '123’)
So basically I set the field with the string to a variable, tried to reverse that field, save the position in variable @pos and replace the string in that position with my string 123.
I get the message: invalid column name CONTEXT
. So I guess this causes trouble @pflist = context,
Thanks a lot guys.
Upvotes: 0
Views: 3403
Reputation: 41
If you need to make such modification for all rows at the table - you could use next script:
select
Left(CONTEXT, LEN(CONTEXT) - CHARINDEX('"',REVERSE(CONTEXT))) + '123' + Right(CONTEXT, CHARINDEX('"',REVERSE(CONTEXT))-1)
from TABLE
Upvotes: 0
Reputation: 2062
Try this :
declare @pf varchar(10), @pos int,
select @pflist = context, @pos = LEN(@pflist) - CHARINDEX('"',REVERSE(@pflist))
UPDATE table SET CONTEXT = CONCAT(LEFT(CONTEXT, CHARINDEX('"',CONTEXT)-1), '123')
Upvotes: 1