How to Remove the Last Character From a Table in SQL?
Last Updated :
10 Dec, 2024
SQL (Structured Query Language) allows for efficient data manipulation and retrieval. A common task in SQL involves removing the last character from a specific column within a table. This can be achieved using string functions like SUBSTRING()
and LEN()
. In this article, we will demonstrate how to achieve this, focusing on a practical example with detailed explanations.
Why Remove the Last Character in SQL?
Removing the last character from a column in SQL is particularly useful in scenarios where data cleanup or formatting is required. For instance, there may be a trailing character, such as a comma or space, that needs to be removed to ensure data consistency. Additionally, we might need to trim specific characters to match a standard format. This operation is also valuable when we need to extract a portion of a string while excluding any unwanted characters, making it easier to maintain clean and usable datasets.
Key SQL String Functions Used
In SQL, string functions are essential for performing various operations on text or string data. Here are some key SQL string functions used in the article, explained in detail:
1. SUBSTRING()
The SUBSTRING() function allows us to extract a specific portion of a string by specifying the starting position and, optionally, the length of the substring. It is commonly used for tasks like isolating a section of a string, such as a name or code, within larger datasets
Syntax
SUBSTRING(string, start_position, length)
Key Terms
- String: It is a required parameter. It is the string on which function is to be applied.
- start_position: It gives the starting position of the string. It is also the required parameter.
- Length: It is an optional parameter. By default, it takes the length of the substring to be returned.
Example
SELECT SUBSTRING('HELLO GEEKS', 1, 5);
Output:
SQLQue
2. LEN():
The LEN() function is used to determine the number of characters in a string, excluding any trailing spaces. It is particularly useful when we need to work with the exact size of a string, such as when trimming unwanted characters or determining the length of data entries
Syntax
LEN(string)
Example
SELECT LEN('SQLQuery');
Output
8
Example of Removing the Last Character in SQL
In this example, we’ll demonstrate how to remove the last character from a string in a column using the SUBSTRING() and LEN() functions.
Step 1: Creating the Table
For the purpose of this demonstration, let’s create a table called demo_table
in a database named geeks
. We will include columns for FIRSTNAME, LASTNAME, and AGE.
Query
CREATE TABLE demo_table
(FIRSTNAME VARCHAR(20),
LASTNAME VARCHAR(20),
AGE INT);
Step 2: Inserting Data into the Table
Next, we’ll insert some sample data into the demo_table
to demonstrate the removal of the last character from the string
Query
INSERT INTO demo_table VALUES
('Romy', 'Kumari', 22 ),
('Pushkar', 'Jha', 23),
('Meenakshi', 'Jha', 20),
('Shalini', 'Jha', 22),
('Nikhil', 'Kalra', 23),
('Akanksha', 'Gupta', 23);
Step 3: Viewing the Data in the Table
Use the following query to display the content of demo_table
:
Query
SELECT * FROM demo_table;
Output

Demo_Table
Example 1: Remove the Last Character from LastName
Now that the table is populated with data, we will use the SUBSTRING() function along with LEN() to remove the last character from the LASTNAME column.
Syntax
SELECT SUBSTRING(column_name,1,LEN(column_name)-1)
FROM table_name;
Query:
SELECT FIRSTNAME, SUBSTRING(LASTNAME,1,LEN(LASTNAME)-1)
AS LASTNAME, AGE FROM demo_table;
Output
FIRSTNAME |
LASTNAME |
AGE |
Romy |
Kumar |
22 |
Pushkar |
Jh |
23 |
Meenakshi |
Jh |
20 |
Shalini |
Jh |
22 |
Nikhil |
Kalr |
23 |
Akanksha |
Gupt |
23 |
Explanation:
SUBSTRING(LASTNAME, 1, LEN(LASTNAME) - 1)
: This expression extracts the string starting from the first character and excluding the last one (by subtracting 1 from the length of the string).
- The result will return the FIRSTNAME, the modified LASTNAME without the last character, and the AGE of each person in the table.
Example 2: Remove the Last Character from FirstName
In this example, we will demonstrate how to remove the last character from the entries in the FIRSTNAME column using the SUBSTRING() and LEN() functions.
Query:
SELECT SUBSTRING(FIRSTNAME,1,LEN(FIRSTNAME)-1)
AS FIRSTNAME, LASTNAME, AGE FROM demo_table;
Output
FIRSTNAME |
LASTNAME |
AGE |
Rom |
Kumari |
22 |
Pushka |
Jha |
23 |
Meenaksh |
Jha |
20 |
Shalin |
Jha |
22 |
Nikhi |
Kalra |
23 |
Akanksh |
Gupta |
23 |
Explanation:
SUBSTRING(FIRSTNAME, 1, LEN(FIRSTNAME) - 1)
: This function extracts the string starting from the first character of FIRSTNAME and removes the last character by subtracting 1 from the length of the string.
- The result will show the modified FIRSTNAME with the last character removed, alongside the LASTNAME and AGE from the table.
Conclusion
String manipulation in SQL is an essential skill for cleaning and formatting data effectively. Removing the last character from a column is a common task that can be efficiently handled using functions like SUBSTRING() and LEN() (or LENGTH() in some databases). These functions allow precise control over string length and content, making it easier to maintain clean and well-structured datasets.
By understanding and applying these techniques, database administrators and developers can enhance data accuracy and streamline operations, ensuring that their databases meet high-quality standards. Mastering such operations not only simplifies routine tasks but also optimizes database performance in the long run.
Similar Reads
How to Remove Last Character from String in Ruby?
Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
How to remove First and Last character from String in Scala?
In this article, we will explore different approaches to removing the first and last character from a string in Scala. Table of Content Using substring methodUsing drop and dropRight methodsUsing substring methodIn this approach, we are using the substring method which takes two arguments the starti
1 min read
How to Remove the First Characters of a Specific Column in a Table in SQL?
Here we will see, how to remove the first characters of a specific column in a table in SQL. We can do this task using the String function. String functions are used to perform an operation on an input string and return an output string. There are various string functions like LEN(for SQL server), S
3 min read
How to remove the first character of string in PHP?
Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation: In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. r
3 min read
How to get the last character of a string in PHP ?
In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, i
2 min read
How to find last value from any table in SQL Server
We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename LAST_VALUE ( scalar_value ) OVER ( [PARTIT
2 min read
Remove Last character from String in Linux
In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman
3 min read
How to Select the Nth Row in a SQLite Database Table?
In SQLite, selecting a specific row from a table can be a common requirement, especially when dealing with large datasets. In this article, we will explore different methods to select the nth row from a SQLite database table. Whether we're a beginner or an experienced developer understanding these m
4 min read
How to Select the nth Row in a SQL Server Database Table?
In SQL Server databases, it's common to encounter scenarios where we need to retrieve a specific row, such as the nth row, from a table efficiently. Whether you're building a pagination feature for a web application or analyzing data, having the ability to select a particular row based on its positi
3 min read
How to Get First Character of a String in SQL?
SQL (Structured Query Language) is essential for managing and querying relational databases. Whether you're handling customer data, employee records, or product details, SQL provides powerful tools for manipulating string data. One common task when working with strings is to extract the first charac
4 min read