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
4000% return in 10 years! ICICI Securities initiates coverage on Bajaj Finance with 'Buy' rating; here's why
Dego Finance (DEGO) Price Prediction 2024, 2025–2030 | CoinCodex
Funny Roblox Id Codes 2023
Valley Fair Tickets Costco
Robinhood Turbotax Discount 2023
Byrn Funeral Home Mayfield Kentucky Obituaries
Jonathan Freeman : "Double homicide in Rowan County leads to arrest" - Bgrnd Search
Oppenheimer & Co. Inc. Buys Shares of 798,472 AST SpaceMobile, Inc. (NASDAQ:ASTS)
Garrick Joker'' Hastings Sentenced
Caroline Cps.powerschool.com
Vichatter Gifs
2135 Royalton Road Columbia Station Oh 44028
Craigslist Jobs Phoenix
Gas Station Drive Thru Car Wash Near Me
Walthampatch
U/Apprenhensive_You8924
Craigslist Farm And Garden Cincinnati Ohio
2016 Ford Fusion Belt Diagram
Used Sawmill For Sale - Craigslist Near Tennessee
25Cc To Tbsp
The Pretty Kitty Tanglewood
north jersey garage & moving sales - craigslist
Who is Jenny Popach? Everything to Know About The Girl Who Allegedly Broke Into the Hype House With Her Mom
Обзор Joxi: Что это такое? Отзывы, аналоги, сайт и инструкции | APS
Timeline of the September 11 Attacks
From This Corner - Chief Glen Brock: A Shawnee Thinker
Dr. Nicole Arcy Dvm Married To Husband
Craigslist Pasco Kennewick Richland Washington
Busted Mugshots Paducah Ky
Emuaid Max First Aid Ointment 2 Ounce Fake Review Analysis
Hypixel Skyblock Dyes
24 slang words teens and Gen Zers are using in 2020, and what they really mean
Obsidian Guard's Skullsplitter
Quake Awakening Fragments
Baywatch 2017 123Movies
Uc Santa Cruz Events
Cranston Sewer Tax
9 oplossingen voor het laptoptouchpad dat niet werkt in Windows - TWCB (NL)
Nba Props Covers
Union Corners Obgyn
Other Places to Get Your Steps - Walk Cabarrus
Tfn Powerschool
Chase Bank Zip Code
Autozone Battery Hold Down
Goats For Sale On Craigslist
Victoria Vesce Playboy
Zeeks Pizza Calories
3367164101
York Racecourse | Racecourses.net
Renfield Showtimes Near Regal The Loop & Rpx
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.