Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQLPLUS SUBSTR

Posted on 2007-04-06
5
Medium Priority
?
4,996 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

705 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