SQL Statement to Remove Part of a String - GeeksforGeeks (2024)

Last Updated : 22 Oct, 2021

Summarize

Comments

Improve

Here we will see SQL statements to remove part of the string.

Method 1: Using SUBSTRING() and LEN() function

We will use this method if we want to remove a part of the string whose position is known to us.

1. 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.

2. LEN(): The syntax is not the standard one. For different server syntax for returning the length of a string may vary. For example, LEN() is in SQL server, LENGTH() is used in oracle database, and so on. It takes only one parameter that is the string whose length you need to find.

Let see these above mention function with an example. Suppose to remove unwanted parts of the string we will extract only the wanted part from string characters from the field, we will use the following query:

Step 1: Create a database

Use the below SQL statement to create 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 creation

We have the following demo_table in our geek’s database.

Query:

CREATE TABLE demo_table(NAME VARCHAR(20),GENDER VARCHAR(20),AGE INT,CITY VARCHAR(20) );

Step 4: Insert data into a table

Query:

INSERT INTO demo_table VALUES('ROMY KUMARI', 'FEMALE', 22, 'NEW DELHI'),('PUSHKAR JHA', 'MALE',23, 'NEW DELHI'),('RINKLE ARORA', 'FEMALE',23, 'PUNJAB'),('AKASH GUPTA', 'MALE', 23, 'UTTAR PRADESH');

Step 5: View data of the table

Query:

SELECT * FROM demo_table;

Output:

SQL Statement to Remove Part of a String - GeeksforGeeks (1)

Step 6: Remove part of a string

Suppose if we want to remove the last 4 characters from the string then, we will extract the remaining part using the below statement.

Syntax:

SELECT SUBSTRING(column_name,1,length(column_name)-4) FROM table_name;

Example :

Remove the last 4 characters from the NAME field.

Query:

SELECT SUBSTRING(NAME,1,len(NAME)-4) AS NAME, GENDER, AGE, CITY FROM demo_table;

Output:

SQL Statement to Remove Part of a String - GeeksforGeeks (2)

Method 2 : Using REPLACE() function

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove.

REMOVE(): This function replaces all occurrences of a substring within a new substring. It takes three parameters, all are required parameters.

  • string Required. The original string
  • old_string Required. The string to be replaced
  • new_string Required. The new replacement string

Syntax:

REPLACE(string, old_string, new_string)

We will use the above demo_table for the demonstration. Suppose if we remove ‘New’ from the CITY field in demo_table then query will be:

Query:

SELECT NAME, GENDER, AGE, REPLACE(CITY,'New','') AS CITY FROM demo_table;

We are not replacing it with a new string.

Output:

SQL Statement to Remove Part of a String - GeeksforGeeks (3)

Method 3: Using TRIM() function

TRIM(): This function removes the space character or other specified characters from the start or end of a string. By using this function we can not remove part of the string from the middle of the string.

Syntax:

TRIM([characters FROM ]string);

We will use the above demo_table for the demonstration. Suppose if we want to remove ‘New’ from the CITY field in demo_table then the query will be as follows:

Query:

SELECT NAME, GENDER, AGE, TRIM ('NEW' FROM CITY)AS "NEW CITY" FROM demo_table;

Output:

SQL Statement to Remove Part of a String - GeeksforGeeks (4)



Please Login to comment...

SQL Statement to Remove Part of a String - GeeksforGeeks (2024)
Top Articles
MCAT Cost in 2024: The Definitive Guide
How to verify your PayPal account to get rid of withdrawal limits on your account
Netr Aerial Viewer
Kreme Delite Menu
Uti Hvacr
Ross Dress For Less Hiring Near Me
Fort Carson Cif Phone Number
Professor Qwertyson
Undergraduate Programs | Webster Vienna
Sportsman Warehouse Cda
Jonathan Freeman : "Double homicide in Rowan County leads to arrest" - Bgrnd Search
라이키 유출
Co Parts Mn
O'reilly's In Monroe Georgia
Lowes 385
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Best Restaurants In Seaside Heights Nj
What is IXL and How Does it Work?
B67 Bus Time
83600 Block Of 11Th Street East Palmdale Ca
Superhot Unblocked Games
The fabulous trio of the Miller sisters
Toy Story 3 Animation Screencaps
Troy Bilt Mower Carburetor Diagram
Satisfactory: How to Make Efficient Factories (Tips, Tricks, & Strategies)
Is The Yankees Game Postponed Tonight
Allentown Craigslist Heavy Equipment
R Baldurs Gate 3
Paradise Point Animal Hospital With Veterinarians On-The-Go
How Much Is An Alignment At Costco
Why Are The French So Google Feud Answers
Haunted Mansion Showtimes Near Cinemark Tinseltown Usa And Imax
15 Downer Way, Crosswicks, NJ 08515 - MLS NJBL2072416 - Coldwell Banker
Scioto Post News
Where Do They Sell Menudo Near Me
Supermarkt Amsterdam - Openingstijden, Folder met alle Aanbiedingen
Wednesday Morning Gifs
What Are Digital Kitchens & How Can They Work for Foodservice
Empires And Puzzles Dark Chest
Craigslist Ludington Michigan
R/Moissanite
Xxn Abbreviation List 2023
Danielle Ranslow Obituary
Craigs List Hartford
Birmingham City Schools Clever Login
Coroner Photos Timothy Treadwell
Verizon Forum Gac Family
Craigslist Pets Charleston Wv
Missed Connections Dayton Ohio
Mmastreams.com
Unbiased Thrive Cat Food Review In 2024 - Cats.com
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 6046

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.