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.
In previous artciles i explained Multiple way to insert multiple records in table in single insert statement and Difference between Temporary Table and Table Variable in Sql Server and Query to count male,female and total employees | Get gender wise employee summary and Multiple queries to get all dates between two dates and SELECT CASE WHEN examples 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:
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..