Open In App

How to Remove the First Characters of a Specific Column in a Table in SQL?

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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), SUBSTR, LTRIM, TRIM, etc. To perform the required function we need the SUBSTRING() functions

SUBSTRING(): 

This function is used to find a sub-string from the string from the given position. It takes three parameters:  

  • String: It is a required parameter. It provides information about the string on which function is applied.
  • Start: 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 whole string.

Query:

SELECT SUBSTRING('geeksforgeeks', 1, 5);

Output:

To delete the first characters from the field we will use the following query:

Syntax:

SELECT SUBSTRING(string, 2, length(string));

Here, string denotes the field, 2 denotes the starting position of string i.e second character, and length(string) denotes the length of the string. 

Query:

SELECT SUBSTRING('geeksforgeeks', 2, len('geeksforgeeks'));

Output:

For the purpose of the demonstration, we will be creating geeks for geeks table in a database called “geeks“.

Step 1: Creating the Database

Use the below SQL statement to create a database called geeks.

Query:

CREATE DATABASE geeks;

Step 2: Using the Database

Use the below SQL statement to switch the database context to geeks.

Query:

USE geeks;

Step 3: Table definition

 We have the following geeks for geeks in our geek’s database.

Query:

 CREATE TABLE geeksforgeeks
 (FIRSTNAME VARCHAR(20),
 LASTNAME VARCHAR(20),
 AGE INT,GENDER VARCHAR(20));

Step 4: Insert data into the table

INSERT INTO geeksforgeeks VALUES
('Romy', 'Kumari', 22, 'female'),
('Pushkar', 'Jha', 23, 'male'),  
('Meenakshi', 'Jha', 20, 'female'),
('Shalini', 'Jha', 22, 'female'),
('Nikhil', 'Kalra', 23, 'male'),
('Akanksha', 'Gupta', 23, 'female');

Step 5: See the content of the table

 Use the below command to see the content of the geeks for geeks table:

Query:

SELECT * FROM geeksforgeeks;

Output:

Step 6: Now to delete the first character from the field we will use the geeks for geeks table. Below is the syntax for the SUBSTRING() function to delete the first character from the field.

Syntax:

SELECT SUBSTRING(column_name,2,length(column_name))
FROM table_name;

To delete the first character from the FIRSTNAME column from the geeks for geeks table. We use the given below query:

Query:

SELECT SUBSTRING(FIRSTNAME,2,len(FIRSTNAME))
FROM geeksforgeeks;

Output:

 

Now to delete the first character from the LASTNAME column.

Query:

SELECT SUBSTRING(LASTNAME,2,len(LASTNAME))AS
LASTNAME FROM geeksforgeeks;

Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads