DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

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

By Ashok Nalam on 14 Dec 2012 | Category: SQL Server | Tagged: string tsql 
We will see how to get and remove last character from a string in sql server.
  Discuss Add Comment   |  
Add rating Rate this resource   

Introduction

In some of cases we need remove last character of string.In this snippet, we will see how to get and remove last character in string.

Sample string: 'DotNetMirror,'

Now we will get last character comma(,) from the string.

Get Last Char in string:

Declare @SampleString varchar(100)
set @SampleString ='DotNetMirror,'
select RIGHT(@SampleString , 1) --output: DotNetMirror

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

  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8666  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.