Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

REGEXP_SUBSTR help

Posted on 2014-09-18
4
Medium Priority
?
218 Views
Last Modified: 2014-10-02
Hello All,

I am trying to clean up an address table with thousands of records.

using
REGEXP_SUBSTR(ADDR_LINE1, '^[^@]+ ')
REGEXP_SUBSTR(ADDR_LINE1, '^[^&]+ ')

I've been able to clean up the addresses that had an @ or & in the middle of addressline1, delete the character and anything after.

Now I need to go through it and do the same for Double spaces '  ' or certain keywords. like

ST
STREET
BETWEEN

This will allow our software to properly geocode these addresses.

the trick with the previous is that unlike @ and &, i do not want to delete the words ST (just what comes afterwords)

I do however want to delete the words BETWEEN and anything that comes afterwords.

thank you for reading
0
Comment
Question by:FutureDBA-
  • 2
  • 2
4 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40331648
PLEASE:  Sample data and expeected results

We can provide specific code for your specific examples.  We have a hard time providing code for general requirements.

Each of your requirements seems doable with individual calls.  The problem is REGEXP functions are very inefficient and costly.

I have a similar requirement for cleaning up strings.  I wrote a custom function that allows me more flexibility than many nested SQL function calls.  It also allows for some 'custom' features that cannot be done with straight SQL.

Here is an example of the SQL for two of your requirements:
--remove everything AFTER BETWEEN
with mydata as
(
select 'This is BETWEEN 0 and 1' mydata from dual
)
select substr(mydata,1,instr(mydata,'BETWEEN')-1) from mydata;


--remove multiple spaces
select regexp_replace('a        b','[ ]+',' ') from dual;

Open in new window

0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40331654
Here is a function that I described that uses the two samples I posted above.

You just need to keep adding more scrubbing requirements to the function.

create or replace function clean_my_data(p_str in varchar2) return varchar2
is
v_str varchar2(4000);
begin

	v_str := p_str;

	--remove everything AFTER BETWEEN
	v_str := substr(v_str,1,instr(v_str,'BETWEEN')-1);

	--remove mutiple spaces
	v_str :=  regexp_replace(v_str,'[ ]+',' ');

	return v_str;

end;
/

show errors


select clean_my_data('Hello             World BETWEEN 1 and 0') from dual;

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40332650
how about something like this?

SELECT addr_line1,
       REGEXP_REPLACE(
           RTRIM(REGEXP_SUBSTR(addr_line1, '^.*?(@|&| ST| BETWEEN|  |$)'), ' @&'),
           'BETWEEN$'
       )
           clean_line
  FROM yourtable
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 40332664
breaking my previous post down...

REGEXP_SUBSTR(addr_line1, '^.*?(@|&| ST| BETWEEN|  |$)')

  read until you find one of the elements in the pipe-delimited list, you can expand that, just make sure you include $ as the last choice

RTRIM(....,' @&')

remove any single character trash from the end

REGEXP_REPLACE(...,'BETWEEN$')

remove whole words from the end

if you want to create a list of words then change it to something like this, be sure to include the same words in the first regexp too

REGEXP_REPLACE(...,'(BETWEEN|SOMEWORD|ANOTHERWORD)$')


As long as your requirements are fairly simple like these the regexp will probably be fine, but if they get complicated you may want to go with a pl/sql solution as slightwv suggested.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

783 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