Solved

SQLPLUS SUBSTR

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 71
Outer Query not returning data - SQL HELP 16 50
PL/SQL Search for multiple strings 5 40
Oracle query output question 4 36
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to recover a database from a user managed backup

910 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

24 Experts available now in Live!

Get 1:1 Help Now