Remove First and Last character of String in SQL Server 2008

In this article I am going to explain how to remove the first and last character from a string in SQL Server 2008.
  • 35413

Introduction

In this article I will explain various queries to remove the first and last character of a string in SQL server 2008. We can remove the characters from simple string or from a column values of a given table. There are various queries explained below to remove first and last character from a string.

1. Remove first character from a string

 To remove first character from a string named "Microsoft", type following query. And result should be like "icrosoft".

Select 'Microsoft' as BeforeRemovalOfFirstCharacter, right ('Microsoft', len ('Microsoft')-1) as FirstCharacterRemoved

Now execute above statement and see output. Fist character i.e: "M" is removed from "Microsoft".

OUTPUT:

Remove_FirstCharacter-from-string.jpg

2. Remove last character from a string

 To remove last character from a string named "Microsoft", type following query. And result should be like "Microsof".

Select 'Microsoft' as BeforeRemovalOfLastCharacter, left ('Microsoft', len ('Microsoft')-1) as AfterRemoveLastCharacter

Now execute above statement and see output.

OUTPUT:

RemoveLastCharacter-from-string.jpg

3. Remove first and last character from string

To remove first and last character from a string named "Microsoft", type following query. And result should be like "icrosof".

select 'Microsoft' as string, left (right ('Microsoft', len ('Microsoft')-1), len ('Microsoft')-2) as AfterRemovalFirstAndLastCharacter;

OUTPUT:

RemoveFirstAndLastCharacter-from-string.jpg


© 2019 DotNetHeaven. All rights reserved.