Removing un-wanted text from strings in Oracle (2024)

Performing string manipulation is very common regardless of the RDBMS that we use. There are lots of string functions available like substr, instr, lpad, rpad etc. to perform string manipulation. We have also covered some of them in our previous blog posts. Recently at the client site, we came across the requirement where we have to remove the ‘-‘ from the phone number string before writing the record to flat file. There are different ways of removing unwanted characters from the string using:

• REPLACE function
• TRANSLATE function
• REGEXP_REPLACE function (10g and above)

We can use REPLACE function to remove the unwanted character from the string. It takes three arguments.

• String from which we want to remove or replace the character.
• Search string.
• Replace string.

SQL> SELECT REPLACE(‘1-770-123-5478′,’-‘,”) COL1 FROM DUAL;

OR

SQL> SELECT REPLACE(‘1-770-123-5478′,’-‘,NULL) COL1 FROM DUAL;

COL1
—————
17701235478

Another way to do this is to use TRANSLATE function in such a way so that we can get rid of unwanted characters. It takes three arguments.
• String from which we want to remove or replace the character.
• Character which we would like to replace or remove from the string in the first argument
• New character with which we want to replace the old character (2nd argument).

Let us see it with example. We will first try to replace ‘-‘ with ‘*’ in the string using translate.

SQL> SELECT TRANSLATE(‘1-770-123-4567′,’-‘,’*’) COL1 FROM DUAL;

COL1
————–
1*770*123*4567

In above example, we replaced character ‘-‘ with ‘*’ but we were talking about removing ‘-‘ from the string. Let us execute following query.

SQL> SELECT TRANSLATE(‘1-770-123-4567’,’@-‘,’@’) COL1 FROM DUAL;

COL1
—————
17701234567

Above result indicates that we are able to remove unwanted character ‘-‘ from the string. In order to understand this, we have to understand how translate works. TRANSLATE function, takes each character from the ‘from string(2nd argument) and replace it with the corresponding character from the ‘to string(3rd argument). In our example, ‘@’ will be replaced with the ‘@’ and ‘-‘ will be replaced with null value since we are not providing any corresponding character for it in the ‘to string’. This will become more clear with the following example.

SQL> SELECT TRANSLATE(‘DECIPHER’,’DE’,’12’) COL1 FROM DUAL;

COL1
—————
12CIPH2R

In above example, occurrence of ‘D’ is replaced with 1 and each occurrence of ‘E’ is replaced with 2.

Somebody might think that why can’t we, use NULL as a replacement character in the 3rd argument. Let us see what result we get when we execute following query.

SQL> SELECT TRANSLATE(‘1-770-123-4567′,’-‘,NULL) COL1 FROM DUAL;

COL1
———-

This is because when we pass NULL argument to TRANSLATE function, it returns null and hence we don’t get the desired result.

Other thing to note about TRANSLATE function is that it is case-sensitive. So if there is a case mismatch, translation will not take place. In following example, we are trying to replace ‘d’ with 1 but ‘d’ does not exist in the string and hence it will not be replaced. Only ‘E’ will be replaced with 2. Query and result is as shown below.

SQL> SELECT TRANSLATE(‘DECIPHER’,’dE’,’12’) COL1 FROM DUAL;

COL1
—————
D2CIPH2R

The way TRANSLATE function differs from REPLACE is that, TRANSLATE function provides single character one to one substitution while REPLACE allows you to replace one string with another.

Starting 10g, Oracle introduced regular expression function REGEXP_REPLACE. We can strip unwanted character from the string using this function as well.

SQL> SELECT REGEXP_REPLACE(‘1-770-123-5478′,’-‘,NULL) COL1 FROM DUAL;

COL1
———–
17701235478

This entry was posted on November 27, 2007 at 11:51 amand is filed under Oracle.You can follow any responses to this entry through the RSS 2.0 feed.Responses are currently closed, but you can trackback from your own site.

Removing un-wanted text from strings in Oracle (2024)
Top Articles
Canada Job Market in 2024: Key Trends, Opportunities & Salaries
How to travel as a student for FREE (or nearly free)
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Soap2Day Autoplay
Pitt Authorized User
Over70Dating Login
Geometry Escape Challenge A Answer Key
Morgan Wallen Pnc Park Seating Chart
Crusader Kings 3 Workshop
Power Outage Map Albany Ny
Sport Clip Hours
zopiclon | Apotheek.nl
Regal Stone Pokemon Gaia
Gas Station Drive Thru Car Wash Near Me
Sam's Club La Habra Gas Prices
Transfer and Pay with Wells Fargo Online®
Nordstrom Rack Glendale Photos
Sizewise Stat Login
Ivegore Machete Mutolation
Www.paystubportal.com/7-11 Login
Craigslist Alo
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
Tactical Masters Price Guide
Gesichtspflege & Gesichtscreme
King Soopers Cashiers Check
Fairwinds Shred Fest 2023
Scat Ladyboy
Calculator Souo
Craigslist In Myrtle Beach
Back to the Future Part III | Rotten Tomatoes
Omnistorm Necro Diablo 4
Buhsd Studentvue
Google Chrome-webbrowser
Dollar Tree's 1,000 store closure tells the perils of poor acquisitions
140000 Kilometers To Miles
Noaa Marine Weather Forecast By Zone
Husker Football
Anguilla Forum Tripadvisor
Wunderground Orlando
COVID-19/Coronavirus Assistance Programs | FindHelp.org
Content Page
Watch Chainsaw Man English Sub/Dub online Free on HiAnime.to
Trending mods at Kenshi Nexus
Port Huron Newspaper
9294027542
Turok: Dinosaur Hunter
Rheumatoid Arthritis Statpearls
Naomi Soraya Zelda
How to Do a Photoshoot in BitLife - Playbite
F9 2385
Hsi Delphi Forum
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6479

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.