Solved

Oracle query

Posted on 2011-09-06
5
288 Views
Last Modified: 2012-08-13
I've this table: TAB_USERS with column USER (name last + first name)

USER
Smith John
Smith David
Smith Jack
Di Martino Giovanni
De Nicola Antonio

I'd like to write a query with only upper (first name).

I tried this query:

select UPPER(substr (USER, instr(USER, ' ')+1))
from TAB_USERS;

But I've some problem with the names: Di Martino Giovanni and De Nicola Antonio

How can I write my query also considering the names with two spaces?

I'd like to get this output:

USER
JOHN
DAVID
JACK
GIOVANNI
ANTONIO
0
Comment
Question by:ralph_rea
  • 4
5 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 36488823
select UPPER(regexp_substr(USER, '[^ ]+$')) from TAB_USERS;

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36488830
select UPPER(substr (USER, instr(USER, ' ',-1)+1))
from TAB_USERS;

0
 

Author Comment

by:ralph_rea
ID: 36488855
Ok, thanks bu now I've another problem, there are some users with this name: Smith John (USA)

How Can I avoid this problem??
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36488896
>> now I've another problem


select UPPER(regexp_substr(replace(USER,'(USA)'), '[^ ]$')) from TAB_USERS;


if you need something more sophisticated than that...

please open a new question,  list all the special parsing rules you want and how to identify them
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 36489409
sorry,  I missed some characters above...

 UPPER(regexp_substr(replace(usr,' (USA)'), '[^ ]+$'))

or, using the instr method...

UPPER(SUBSTR(REPLACE(usr, ' (USA)'), INSTR(REPLACE(usr, ' (USA)'), ' ', -1) + 1))
0

Featured Post

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.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now