Remove first or last character from string or column in sql server

Introduction: In this article I am going to share how to remove first or last character/letter from string or table column in sql server.


Description: While working there may be requirement to remove first or last character from string. I have mentioned two ways to get this done: Using Sql inbuilt LEFT and RIGHT or SUBSTRING functions.

Implementation: Let’s write sql queries to demonstrate the concept. 

Remove first character from a string or column using SQL Right Function

Example 1:  Remove first character from a string
DECLARE @str VARCHAR(50)='Computer'
SELECT RIGHT(@str, LEN(@str)-1) AS Result

Query result will be : 
Result
omputer

Example 2: Remove first character from table column. Let’s create a temporary table and insert some data into it.
CREATE TABLE #tbItem
(
                ItemId  INT IDENTITY(1,1) NOT NULL,
                ItemName          VARCHAR(100),
                ItemPrice   DECIMAL(10,2) 
)

INSERT INTO #tbItem values('Soap',24.00),('Tooth Paste', 85.50), ('Hair Oil', 260.00),('Door Mat',300.00)

Now the query to remove first character of all the items present in the table will be as:
SELECT RIGHT(ItemName, LEN(ItemName)-1) AS ItemName,ItemPrice FROM #tbItem

DROP table #tbItem

Query result will be : 
ItemName
ItemPrice
oap
24.00
ooth Paste
85.50
air Oil
260.00
oor Mat
300.00

Remove first character from a string or column using Substring Function

Example 1:  Remove first character from a string
DECLARE @str VARCHAR(50)='Computer'
SELECT SUBSTRING(@str, 2, LEN(@str)-1) AS Result

Query result will be : 
Result
omputer
  
Example 2: Remove first character from table column.
CREATE TABLE #tbItem
(
                ItemId  INT IDENTITY(1,1) NOT NULL,
                ItemName          VARCHAR(100),
                ItemPrice   DECIMAL(10,2) 
)

INSERT INTO #tbItem values('Soap',24.00),('Tooth Paste', 85.50), ('Hair Oil', 260.00),('Door Mat',300.00)

SELECT SUBSTRING(ItemName, 2, LEN(ItemName)-1) AS ItemName,ItemPrice FROM #tbItem

DROP table #tbItem

Query result will be : 
ItemName
ItemPrice
oap
24.00
ooth Paste
85.50
air Oil
260.00
oor Mat
300.00

Remove last character from a string or column using SQL Left Function

Example 1:  Remove last character from a string
DECLARE @str VARCHAR(50)='Computer'
SELECT LEFT(@str, LEN(@str)-1) AS Result

Query result will be : 
Result
Compute

Example 2: Remove last character from table column
CREATE TABLE #tbItem
(
                ItemId  INT IDENTITY(1,1) NOT NULL,
                ItemName          VARCHAR(100),
                ItemPrice   DECIMAL(10,2) 
)

INSERT INTO #tbItem values('Soap',24.00),('Tooth Paste', 85.50), ('Hair Oil', 260.00),('Door Mat',300.00)

SELECT LEFT(ItemName, LEN(ItemName)-1) AS ItemName,ItemPrice FROM #tbItem

DROP table #tbItem

Query result will be : 
ItemName
ItemPrice
Soa
24.00
Tooth Past
85.50
Hair Oi
260.00
Door Ma
300.00

Remove last character from a string or column using Substring Function

Example 1:  Remove last character from a string
DECLARE @str VARCHAR(50)='Computer'
SELECT SUBSTRING(@str, 1, LEN(@str)-1) AS Result

Query result will be : 
Result
Compute

Example 2: Remove last character from table column
CREATE TABLE #tbItem
(
                ItemId  INT IDENTITY(1,1) NOT NULL,
                ItemName          VARCHAR(100),
                ItemPrice   DECIMAL(10,2) 
)

INSERT INTO #tbItem values('Soap',24.00),('Tooth Paste', 85.50), ('Hair Oil', 260.00),('Door Mat',300.00)

SELECT SUBSTRING(ItemName, 1, LEN(ItemName)-1) AS ItemName,ItemPrice FROM #tbItem

DROP table #tbItem

Query result will be : 
ItemName
ItemPrice
Soa
24.00
Tooth Past
85.50
Hair Oi
260.00
Door Ma
300.00
Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..