How to search or replace string from right side
May.13, 2009 in
SQL
Assume that you have comma separated names list and you want to replace last comma sign by string ‘and’, you can reverse string value and find the char index and do it as below(or download Script).
DECLARE @StringList varchar(200)
DECLARE @index INT
DECLARE @lastWord varchar(100)
SET @StringList = ‘Aneef,Ajith,Gayan,Manoj,Jude,Julius’
SELECT @index=charindex(’,', reverse(@StringList))
SELECT @lastWord=right(@StringList,charindex(’,', reverse(@StringList)) -1)
SELECT SUBSTRING(@StringList,1,LEN(@StringList)-@index)+’ and ‘+@lastWord


Leave a Reply