Link to home
Create AccountLog in
Avatar of Basssque
Basssque

asked on

Address column split and case when statements

I'm currently using the following statement to split address columns
SUBSTR(STREET, 1, INSTR(STREET, ' ')-1) AS HouseNumber,
SUBSTR(STREET, INSTR(STREET, ' ')+1) AS Street,

In this case, 50 Main St is split into two columns
50, Main St

Is it possible to add a third column to split letters out from the house number?
In this case 50D Main St is out put like 50,D,Main St AND 50 Main St would be output like 50,,Main St

I also need to strip leading spaces from the Street after the split or replace instances of more than one space with a single space prior to the split

Last, if I have an House_Number values of
100
200
50
How can I sort them in order so 50 comes before 100 opposed to after 200?

Thanks!!!
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Based on your previous data scrubbing questions, please take a look through your data for other possibilities.

See if you have anything like:
123D-45 Main Street
Apt 1 Main St
etc...

Then give us the expected results from them.
Avatar of Basssque

ASKER

Already done :-)
The examples I provided are a the only factors that need to be accounted for
Try this
select regexp_substr(street,'^[0-9]+') col1,
trim(regexp_substr(street,'[A-Za-z]+ ')) col2,
trim(regexp_substr(street,' .*$')) col3
from some_table
/
If no letter exists after the number but before the space, it outputs the first word in the street name opposed to null

Doesn't sort column1 as noted
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Looks really good.
Only think missing is accounting for and removing leading spaces from Col3
They still exist which pushes street names with leading spaces up to the top of the column.
>>Only think missing is accounting for and removing leading spaces from Col3

Just add the TRIM to the columns that need them.  I forgot to add them back to my test case on my latest post.
With cte as
(select
 SUBSTR(STREET, 1, INSTR(STREET, ' ')-1) AS housenumber,
 SUBSTR(STREET, INSTR(STREET, ' ')+1) AS Street
 from yourtable)
select trim(regexp_substr(housenumber,'[0-9]+[^a-zA-Z]')) as housenumber,
trim(regexp_substr(housenumber,'[^0-9]')) as houseletter,
trim(street) as street
from cte
order by to_number(housenumber);
Whoa, sorry. I'm way behind. Spent too much time typing and testing along with outside interference :-)
Works with the trim's!  Thanks again for your time!!!