Link to home
Start Free TrialLog in
Avatar of iamnamja
iamnamja

asked on

Oracle SQL - Extract rightmost word in string

Hi, I need to extract the right-most word from a field that could have multiple spaces. Example: "Administration Main Boston" = "Boston"

Thanks.
Avatar of Sean Stuber
Sean Stuber

regexp_substr(yourstring,'[^ ]+$')


for example...


select regexp_substr('Administration Main Boston','[^ ]+$') from dual
SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
substr/instr method:
select substr('administration main boston', instr('administration main boston',' ',-1)+1) from dual;
Avatar of iamnamja

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
another interesting variation for 2nd last word

reverse(regexp_substr(reverse('Administration Main Boston Office'),'[^ ]+',1,2))
you should awared a split with slightwv as well for answering the original question