Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

02 November 2012

GET/Remove the last character from a string in SQL Server

In some of cases we need remove last character from the string.In this tip we will see how to get and remove last character in string.

Sample string: 'DotNetMirror,'

Now we will remove , from the string.

Get Last Char in string:


Declare @STRING varchar(100)
set @STRING='DotNetMirror,'

select RIGHT(@String, 1)
Remove the last char(comma) in string :

IF(LEN(@String)> 0 AND RIGHT(@String, 1) = ',')
BEGIN
select substring(@String, 1, (len(@String) - 1)) --or
SET @String = LEFT(@String, LEN(@String) - 1)
END
select @String