Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 581
Imran Ali Khan Avatar asked Oct 30 '14 07:10

Imran Ali Khan


2 Answers

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

DECLARE @MyWORD VARCHAR(20) = 'ABCDEF123'

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

output:

ABCDEF122
like image 195
Dan Field Avatar answered Oct 31 '22 20:10

Dan Field


Try this.

SELECT LEFT('ERMN0', Len('ERMN0')-1)
       + Replace(RIGHT('ERMN0', 1), 0, 1) 

OUTPUT : ERMN1

In your case

SELECT LEFT(MYWORD, Len(MYWORD)-1)
       + Replace(RIGHT(MYWORD, 1), 0, 1) as [REPLACED] FROM MYTABLE
like image 30
Pரதீப் Avatar answered Oct 31 '22 21:10

Pரதீப்