Oracle SQL - Extract rightmost word in string

Posted on 2014-01-27
Last Modified: 2014-01-27
Hi, I need to extract the right-most word from a field that could have multiple spaces. Example: "Administration Main Boston" = "Boston"

Question by:iamnamja
  • 4
  • 2
LVL 73

Expert Comment

ID: 39812966
regexp_substr(yourstring,'[^ ]+$')

for example...

select regexp_substr('Administration Main Boston','[^ ]+$') from dual
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 200 total points
ID: 39812967
Several ways.

Try this one:
select regexp_substr('administration main boston','[^ ]+$') from dual;
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39812976
substr/instr method:
select substr('administration main boston', instr('administration main boston',' ',-1)+1) from dual;
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 39812985
Thank you both so much for the function.
One more question: what if I need to extract 2nd right most word? Just realized, I need to do this for some conditions. Is there a quick way to extract 2nd right most word?

"Administration Main Boston Office" = "Boston"

Thank you.
LVL 73

Accepted Solution

sdstuber earned 300 total points
ID: 39813003
regexp_substr('Administration Main Boston Office','([^ ]+) *[^ ]+$',1,1,null,1)

or, if your version doesn't support substr back references

regexp_substr(regexp_substr('Administration Main Boston Office','[^ ]+ *[^ ]+$'),'[^ ]+')
LVL 73

Expert Comment

ID: 39813010
another interesting variation for 2nd last word

reverse(regexp_substr(reverse('Administration Main Boston Office'),'[^ ]+',1,2))
LVL 73

Expert Comment

ID: 39813016
you should awared a split with slightwv as well for answering the original question

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FRM-40735:KEY-COMMIT trigger raised unhandled exception ORA-01422 7 68
report returning null 21 92
Fastest way to replace data in Oracle 5 62
Converting a row into a column 2 51
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question