Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Query to remove first, last character from string in sql server

Jul 21, 2012
Introduction:

In this article I will explain how to remove first or last characters from string in SQL server.

Description:

In previous post I explained concatenate row values into string and substring function example to replace particular of string in SQL Server and many articles relating to SQL Server. Now I will explain how to write a query to remove first or last character from string in SQL Server. Here I will explain with one example for that I have one string which contains data like this

Here I want to remove first character from string and need to get remaining string values for that we need to write a query like as show below

Syntax to Remove First character from string


SELECT SUBSTRING(ColumnName,2,(LEN(ColumnName))) FROM TABLENAME
Ex:


DECLARE @name NVARCHAR(MAX)
SET @name=',SureshDasari,MaheshD,MadhavS,PrasanthiD'
SELECT Substring(@name, 2, (len(@name))) as UserNames
OutPut

Syntax to Remove Last character from string

If we want to remove last character from string we need to write query like as shown below

SELECT SUBSTRING(ColumnName,1,(LEN(ColumnName)-1)) FROM TABLENAME
Ex:


DECLARE @name NVARCHAR(MAX)
SET @name='SureshDasari,MaheshD,MadhavS,PrasanthiD,'
SELECT Substring(@name, 1, (len(@name)-1)) as UserNames
OutPut

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

* MOON * said...

nice

* MOON * said...

All ur post are so useful to me ...

Anonymous said...

Thanks a lot for valuable example..
Regards,
Nishant Giri

Shiva Katta said...

declare @sample varchar(30)
set @sample=',siva,'
select substring(substring(@sample,2,len(@sample)),1,len(substring(@sample,2,len(@sample)))-1)

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.