Solved

SQLPLUS SUBSTR

Posted on 2007-04-06
5
4,925 Views
Last Modified: 2013-12-19
how do I get the first name out of a string like this with sql plus in oracle.
DOE-DOE, JOHN-XXXX
JOHN would be the first name.
0
Comment
Question by:fusebox
5 Comments
 
LVL 7

Expert Comment

by:gattu007
ID: 18867348
I dont think using substar you can know all the first names... coz substar will not know the number of characters....

you can first count the number of characters... either programetically

The "substring" functions return a portion of string, beginning at character position, substring_length characters long. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. SUBSTRC uses Unicode complete characters. SUBSTR2 uses UCS2 codepoints. SUBSTR4 uses UCS4 codepoints.

The following example returns several specified substrings of "ABCDEFG":

SELECT SUBSTR('ABCDEFG',3,4) "Substring"
     FROM DUAL;
 
Substring
---------
CDEF

SELECT SUBSTR('ABCDEFG',-5,4) "Substring"
     FROM DUAL;

Substring
---------
CDEF


http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions119a.htm#1004186
0
 

Author Comment

by:fusebox
ID: 18867583
I was thinking that it should be something like substr(column,instr(column but not sure of the exact sequence.
0
 
LVL 12

Accepted Solution

by:
jwahl earned 500 total points
ID: 18868848
yes, it works with SUBSTR and INSTR combinations, but it's hard to read:

1)
INSTR('DOE-DOE, JOHN-XXXX', ', ') +2 ... beginning of code to extract:

SQL> SELECT SUBSTR('DOE-DOE, JOHN-XXXX', INSTR('DOE-DOE, JOHN-XXXX', ', ') + 2) FROM dual;

JOHN-XXXX

2)
INSTR('DOE-DOE, JOHN-XXXX', '-', -1) ... absolute position of ending code to extract:

SQL> SELECT INSTR('DOE-DOE, JOHN-XXXX', '-', -1) FROM dual;

14

3)
length of the string:

SQL>  SELECT LENGTH('DOE-DOE,JOHN-XXXX') FROM dual;

17

4)
finally the complete set:

SQL>
SELECT SUBSTR('DOE-DOE, JOHN-XXXX',
       INSTR('DOE-DOE, JOHN-XXXX', ', ') + 2,
       LENGTH('DOE-DOE, JOHN-XXXX') - INSTR('DOE-DOE,JOHN-XXXX','-',-1) - 1
       )
FROM dual;

JOHN

but i'd recommend to write a stored procedure or function ...





0
 
LVL 32

Expert Comment

by:awking00
ID: 18869894
As stated herein, the solution is going to be some combination of substr and instr depending on the structure of the data. If the first name ALWAYS starts after the ", " (comma, space) and ALWAYS before the last "-" (hyphen), then jwahl's proposed solution will work fine. If the possibility exists that the data can have a different structure (e.g. there is no hyphen after the first name), then more diagnostics will be required to cover all of the possibilities. If you know of all the possible data formats, post them here and we'll try and work out a suitable query.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 18875928
Writing a stored procedure is the best way to implement these type of logics because they give you the flexibility to debug and even for code changes.

We can do anything with a sql query but that cannot be used for reengineering later. I mean later on if someone has to work on that sql, it will be difficult for understanding/complex will be more because we might have tried to each include possibility of data and so there is a very good chance of the sql statement growing bigger and bigger everyday to accomodate logic for new cases discovered over a period of time.

Thanks
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

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ā€¦
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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 copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

820 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