Imran Ali Khan Imran Ali Khan - 1 month ago 10
SQL Question

Replace Last character in SQL Server 2008

I am working with SQL server 2008, and facing problem about character replacement.

If I use

SELECT REPLACE(MYWORD,0,1) FROM MYTABLE


It is replacing all 0 into 1, I just want to replace Last character Like
MYWORD = "ERMN0"
so it will be
MYWORD = "ERMN1"

Answer

using STUFF, which, IMO, ends up being most readable:

DECLARE @MyWORD VARCHAR(20) = 'ABCDEF123'

SELECT STUFF(@MyWORD, LEN(@MyWORD), 1, '2')

output:

ABCDEF122
Comments