Solved

Oracle SQL: Selecting segments of a character string with pipe separators

Posted on 2013-01-10
7
350 Views
Last Modified: 2013-01-11
Hello

I'd like to know how best to write some SQL that allows me to select a portion or portions of a character string with pipe separators. For example, the string could look like:

"Name|Address|Telephone Number|email address"

So I would like to able to say something like:

select "portion 3 from character string"
from "table name"
where "field name" is not null (--this is a constant field name where the data is stored)

Any help most welcome
0
Comment
Question by:rich02
  • 2
  • 2
  • 2
7 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38762976
see this code:
select REGEXP_SUBSTR('Name|Address|Telephone Number|email address'
  ,'[^|]+', 1 , 1 ) x1
, REGEXP_SUBSTR('Name|Address|Telephone Number|email address'
  ,'[^|]+', 1 , 2 ) x2
REGEXP_SUBSTR('Name|Address|Telephone Number|email address'
  ,'[^|]+', 1 , 3 ) x3
   from dual

Open in new window

0
 
LVL 6

Expert Comment

by:Javier Morales
ID: 38766187
Hi,

In addition to angelIII solution, a simple function would solve the coding as you're requesting in your question.

create or replace function get_portion (text varchar2, 
                   portion number) return varchar2
is 
      returning_text varchar2(4000);
begin
         returning_text:=REGEXP_SUBSTR(text,'[^|]+', 1 ,portion);
         return (returning_text);
end; 
/

Open in new window


This way, the creation and coding would be as shown below.

SQL> create or replace function get_portion (text varchar2, portion number) return varchar2
  2  is 
  3        returning_text varchar2(4000);
  4  begin
  5           returning_text:=REGEXP_SUBSTR(text,'[^|]+', 1 ,portion);
  6           return (returning_text);
  7  end; 
  8  /

Function created.

SQL> select get_portion('Name|Address|Telephone Number|email address',3) from dual;

GET_PORTION('NAME|ADDRESS|TELEPHONENUMBER|EMAILADDRESS',3)
--------------------------------------------------------------------------------
Telephone Number

Open in new window


Now you can use that funcion as you wanted in your question, as part of SELECT or WHERE clauses.

select get_portion (character_string, 3)
from "table name"
where "field name" is not null;

Hope this helps,
Javier
0
 

Author Comment

by:rich02
ID: 38766390
Hello Javier and angelIII

Both solutions work perfectly. Very much appreciated. Thanks
0
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.

 

Author Comment

by:rich02
ID: 38766491
I've requested that this question be closed as follows:

Accepted answer: 0 points for rich02's comment #a38766390

for the following reason:

Both solutions were very effective and easy to follow.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38766492
then please accept our comments and not yours :)
glad we could help
0
 
LVL 6

Expert Comment

by:Javier Morales
ID: 38766524
yes ! :)

you're welcome ! :)  glad we could help ! :)
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
compre toata in where clue oracle 4 56
query returning everything 11 87
SQL query question 8 36
1 FROM DUAL wont work with additional columns ?? 4 22
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 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…

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

20 Experts available now in Live!

Get 1:1 Help Now