Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







SQL to find the previous word in a text string

Oracle Database Tips by Donald BurlesonAugust 27,  2015

Question:  I need to write SQL to parse a text string and display the word that is previous to my tag (XXX).  I have a table like this, where each word is separated by an underscore:

ID (int), Field1 (varchar2)
1, abc_de_XXX_fg
2, abcd_e_XXX
3, a_b_XXX_c_d
4, a_b_cde
5, XXX_a_b_c
6, abcdef_XXX_ghij_klm_n


I need SQL to find previously word in the string ("_" is the word separator) before the XXX.  The results of finding the previous word are:


ID (int), Field1 (varchar2), RESULT
-- -------------          ---
1, abc_de_XXX_fg,         de
2, abcd_e_XXX,            e
3, a_b_XXX_c_d,           b
4, a_b_cde,               null
5, XXX_a_b_c,             null
6, abcdef_XXX_ghij_klm_n, abcdef

Answer:  Yes, you can write SQL to parse a string, locate a particular tag, and print a string relative to the location of the text tag.

In this example, the SQL uses the instr function to locate the tag and then the reverse function to locate the previous word in the text.  Of course, this can be modified to replace the underscore with spaces to locate ordinary words within a text string:

select ID,
                                      instr(field1, '_XXX') - 1)),
                                            instr(field1, '_XXX') - 1)),
                             '_') - 1),
                reverse(substr(field1, 0, instr(field1, '_XXX') - 1))))
  from MY_TABLE;



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.