Solved

SQLPLUS SUBSTR

Posted on 2007-04-06
5
4,877 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 31

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.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

708 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

19 Experts available now in Live!

Get 1:1 Help Now