user3659470
user3659470

Reputation: 21

Sybase SQL: Find last character in a string and replace it

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.

Now 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

Answers (2)

Mikhail Zankovich
Mikhail Zankovich

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

Amol Bavannavar
Amol Bavannavar

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

Related Questions