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
Unicoin Launches Its Primary Offering on U.S. Regulated INX.One Trading Platform
Introduction To The Federal Court System
WALB Locker Room Report Week 5 2024
Boomerang Media Group: Quality Media Solutions
Sarah F. Tebbens | people.wright.edu
Sissy Transformation Guide | Venus Sissy Training
The Idol - watch tv show streaming online
Arrests reported by Yuba County Sheriff
Directions To 401 East Chestnut Street Louisville Kentucky
David Packouz Girlfriend
Category: Star Wars: Galaxy of Heroes | EA Forums
Graveguard Set Bloodborne
Whiskeytown Camera
83600 Block Of 11Th Street East Palmdale Ca
Comenity Credit Card Guide 2024: Things To Know And Alternatives
U.S. Nuclear Weapons Complex: Y-12 and Oak Ridge National Laboratory…
Dusk
Nier Automata Chapter Select Unlock
Hartland Liquidation Oconomowoc
Buff Cookie Only Fans
Vanessa West Tripod Jeffrey Dahmer
Elemental Showtimes Near Cinemark Flint West 14
Kiddle Encyclopedia
Gia_Divine
Why Should We Hire You? - Professional Answers for 2024
Allentown Craigslist Heavy Equipment
Craigslist Clinton Ar
Baja Boats For Sale On Craigslist
12 Facts About John J. McCloy: The 20th Century’s Most Powerful American?
Sadie Sink Reveals She Struggles With Imposter Syndrome
Southwest Flight 238
Defending The Broken Isles
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
130Nm In Ft Lbs
Tamil Movies - Ogomovies
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Loopnet Properties For Sale
Quality Tire Denver City Texas
State Legislatures Icivics Answer Key
Obituaries in Hagerstown, MD | The Herald-Mail
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Pink Runtz Strain, The Ultimate Guide
Memberweb Bw
Chase Bank Zip Code
Costco The Dalles Or
St Anthony Hospital Crown Point Visiting Hours
Hello – Cornerstone Chapel
Bismarck Mandan Mugshots
Fresno Craglist
Strawberry Lake Nd Cabins For Sale
Craigslist Charlestown Indiana
Palmyra Authentic Mediterranean Cuisine مطعم أبو سمرة
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.