Solved

SQLPLUS SUBSTR

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

Suggested Solutions

Title # Comments Views Activity
Oracle DATE Column Space 11 79
sort a spool into file output in oracle 1 28
Oracle SQL Select unique values from two columns 4 53
oracle- set role and grant privileges 6 28
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

770 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