Split Out An Address String in Crystal Reports 11

I need to split out an address string which is made up of the street number and name into several pieces:
1.  Street number
2. Street direction
3. Apt number
4. Street name
Example 1: 123 NW Anywhere 345

I need '123' in the "Street Number" column, 'NW' in the "Street Direction" column, 'Anywhere' in the "Street name" column and "345" in the "Apt number" column.  

I've tried splitting out the field with a space as the delimiter, but I am getting errors when I try to split the field beyond 3 because not all the address records in this file have beyond two splits.

Any ideas on how to accomplish this?
rrtraverseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alces2Commented:
Assuming you use the Split function: If you do not include the "count" parameter, doesn't it give you an array with as many parts as it finds?

That doesn't help you with blanks in street names like 123 N. Martin Luther King Dr., but it should get rid of the count problem.

Then you would have to apply some Google maps logic in, for example, having a list of valid street directions (Like N, S, NW...) and/or look at the position of the value in the array. Say, the first value most likely won't be the street name and so forth.

It gets tricky if you have to cover all the special cases, like people who put "Apt. 123" in or "#123". If you have control over the data and can eliminate those cases, even better.
0
Alces2Commented:
To clarify:
With "Google maps logic" I didn't mean using any of their services, I just meant for your code to be able to identify what array value resembles what part of the address. Like you can type almost anything in Google maps and it picks the right address for you :)
0
rrtraverseAuthor Commented:
Can you please give me an example of syntax for the 'strip' statement related to how to pull in the balance of an address after splitting out the adress by space delimeter and stripping out the street and direction part of the address?

Thanks
0
rrtraverseAuthor Commented:
This is the split statement I am currently using.  
local stringvar input := {address};
if instr(input, " ") >0
Then split(input, " ")[3]
else input
0
Alces2Commented:
you could use the following:

stringVar array  address_parts := split ({Address}, " ");

That splits up whatever is in the field and puts it in the array.
So sometimes the array will have 4 values (with apt.), sometimes 3 (without apt).

You can check the number of elements in the array with:
count (address_parts);

Then check the value. If it's 4, you have an apartment. Whereas this simple example assumes that all street names are one word. That's why I said you would need to check the single array elements.

Crystal also seems to complain a lot when trying to access elements that are not there, e.g. the 4 th value, but the array has only three elements. If you don't check for these things, you get errors all the time.

This should work, but may not be the most efficient in terms of processing time.
I included a check if the field is null in here:

For the first part (first column):

if not isnull({Address}) then(
    stringVar array  address_parts := split ({Address});
    if count(address_parts) >= 1 then  //make sure there is at least the first element there
        address_parts[1];  // take the first element and display
    )
else
"John has a big nose"  // mock John if there is a null value


Then the formula for the second column would then be:

if not isnull({Address}) then(
    stringVar array  address_parts := split ({Address});
    if count(address_parts) >= 2 then  //make sure there is at least the second element there
        address_parts[2];  // take the second element and display
    )
else
"Judy has big feet" // mock Judy if there is a null value


and so forth, increasing each number and checking the next value, then displaying that formula field on the report.

If you don't need separate fields, you could have a loop from 1 to count(address_parts) and do you thing in the loop.

You still need to make sure that street names with blanks in it are then especially handled (say, joined again). A simple solution could be to check the last element in the array. If it only contains numbers, it's assumed to be an apt.. If not, it's part if the street name.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.