?
Solved

Oracle procedure logic - string manipulation

Posted on 2012-12-28
17
Medium Priority
?
640 Views
Last Modified: 2012-12-29
I have a view where I have to do a little string manipulation. I have a text field where I just want to bring back the text in the field after the last pipe | . For instance '~(`|¤|football)~'
I just want to bring back football. There is commonality to the data because they all start with '~( and end with )~'. Also, the data that I need will always be after the last pipe |.
Another example would be '~(`|¤|{link:10098}|Mobile, Alabama,)~' would return
Mobile, Alabama,

Please let me know if you have any questions.

Thanks everyone
JK
0
Comment
Question by:jknj72
[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
  • 7
  • 5
  • 3
  • +2
17 Comments
 
LVL 14

Expert Comment

by:leoahmad
ID: 38726801
select substr(yrcol,instr(yrcol,'|',-1),instr(yrcol,')')-2) from yrtab
0
 

Author Comment

by:jknj72
ID: 38726842
this            '~(`|¤|{link:10098}|Mobile, Alabama,)~'
returns       |Mobile, Alabama,)~

I think I still need a bit of tweaking
0
 

Author Comment

by:jknj72
ID: 38726990
Really could use some help here........
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38727130
In MS SQL syntax it will be somethingn like

DECLARE @Str VARCHAR(MAX)
SET @Str = '~(`|¤|{link:10098}|Mobile, Alabama,)~'
--SET @Str =  '~(`|¤|football)~'

SELECT 
	 @Str
	,REPLACE(RIGHT(@Str, CHARINDEX('|', REVERSE(@Str))- 1), ')~', '')
	

Open in new window


Dont have Oracle installed otherwise I would modify it
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38727577
regexp_substr(str,'.*\|(.+)\)~$',1,1,null,1)
0
 

Author Comment

by:jknj72
ID: 38727633
I am getting an error executing ; regexp_substr(str,'.*\|(.+)\)~$',1,1,null,1)

  Execution (28: 8): ORA-00939: too many arguments for function

It seems that there are 6 arguments in your example and looking online I see examples that use are 4(?)

Any ideas?
0
 
LVL 32

Expert Comment

by:awking00
ID: 38727665
sdstuber,
Would you be kind enough to explain the pattern match and what each of the remaining four parameters are? I typically think of the regexp_substr function as having a maximum of five parameters, the string expression, the pattern, the start position, the occurrence, and a match parameter but you're showing six. Great job, by the way!
0
 
LVL 32

Expert Comment

by:awking00
ID: 38727675
It worked for me.
SQL> select * from txt;
TEXT
-------------------------------------------------------------
~(`|¤|football)~
~(`|¤|{link:10098}|Mobile, Alabama,)~

SQL> select regexp_substr(text,'.*\|(.+)\)~$',1,1,null,1)
  2  from txt;
REGEXP_SUBSTR(TEXT,'.*\|(.+)\)~$',1,1,NULL,1)
-------------------------------------------------------------
football
Mobile, Alabama,
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38727799
The last parameter (subexpression) is for 11gR2.  If you have a lower version you'll get the error you see.

the parameters are...

str,   - the string you are searcing
'.*\|(.+)\)~$'  - the regular expression describing what we are looking for
,1  - starting at the first character
,1 - find the first instance of the expression
,null - no special options like case insensitive or multi-line
,1 - return only the first subexpression
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 38727808
this should work in 10g and above

REGEXP_REPLACE(REGEXP_SUBSTR(yrcol, '[^|)]+\)~$'), '\)~$')

or this...


 REPLACE(SUBSTR(yrcol, INSTR(yrcol, '|', -1) + 1), ')~')
0
 

Author Comment

by:jknj72
ID: 38727859
Nicely done sdstuber!!  Thank you very much!!
0
 

Author Closing Comment

by:jknj72
ID: 38727860
great work
0
 
LVL 32

Expert Comment

by:awking00
ID: 38727881
Thanks so much for the explanation. We recently upgraded to 11gR2, which was why it worked, but was looking at 11gR1 documentation. Regular expressions aren't very intuitive, but extremely powerful when you get them right :-)
0
 

Author Comment

by:jknj72
ID: 38727910
I think I may have screwed up. Its not working 100%, I will post another question related to this one
0
 

Author Comment

by:jknj72
ID: 38727959
please see ID: 27979957
0
 
LVL 32

Expert Comment

by:awking00
ID: 38729549
This query takes all characters before the '~ (whether or not they exist) and concatenates them with the function sdstuber provided against all of the characters between the '~ and ~' and concatenates them with all of the characters after the ~'  (whether or not they exist), adding a space between the three parts then trimming any outside spaces -

select trim(substr(text,instr(text,'~'||chr(39)) + 2)||' '||
REPLACE(SUBSTR(REGEXP_SUBSTR(text, '~[^~]+~'), INSTR(REGEXP_SUBSTR(text, '~[^~]+~'), '|', -1) + 1), ')~')||' '||
substr(text,instr(text,'~'||chr(39)) + 2)) mystring
from txt;

MYSTRING
-------------------------------------------------------
football
Mobile, Alabama,
blahblahblah football blahblahblah
0
 
LVL 32

Expert Comment

by:awking00
ID: 38729550
I meant to post this to your other question, which I have now done.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

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