Solved

stripping data from a Varchar feild in Crystal 2008

Posted on 2012-12-26
6
232 Views
Last Modified: 2012-12-27
Good Morning Experts,

I have a field in a SQL 2005 database that is "Address Line 1" which contains street number, street name and in some cases unit numbers as well (sometimes proceeded by a comma, someties not. I need  to export this data as two or if unit number exists then three seperate feilds, "Street Number" "Street Name" and "Unit Number" and I need to remove the comma if one exists as this will be sent as a comma delimeted file in the end.  In addition the numeric portion of the address for example "123 anystreet" sometimes will contains a dash, for example "12-24 anystreet".  I need to maintain the dash character in it's existing location.  Any ideas?

To give an example of the variations I've seen in the data I've got:
"123 anystreet"
"123 anystreet, Unit 1"
"123 anystreet Unit 1"

"123-45 anystreet"
"123-45 anystreet, Unit 1"
"123-45 anystreet Unit1"
"123-45 anystreet Unit 1c"
"123-45 anystreet unit 1-3c & 2-6b"
0
Comment
Question by:ISBTECH
6 Comments
 
LVL 2

Expert Comment

by:rmail
ID: 38721239
You could break the string on the first space and the word 'unit':
http://stackoverflow.com/questions/697519/split-function-equivalent-in-tsql
0
 
LVL 8

Expert Comment

by:hpdvs2
ID: 38721268
There is no clean way to do this, because some people may use "Apt", "Suite", etc... or skip a prefix altogether and give an address like this:

3326 Adler Lane B  
instead of
3326 Adler Lane, Apt B
or
3326 Adler Lane Ste B

If you only had a few things to worry about, like Apt or Unit, I would say go for writing your own code to sort it, but there are so many variations that could exist that it would be tough, with a strong chance of errors that won't be caught with addresses that will get mangled.

So, 2 things

1) Reconsider.  What value is there to having these records separated.  Is there something your company does that will strongly benefit from doing this uncommon practice?  I've had times that I considered it, but it never served any real value.  There is a phrase for garbled data like this, "Garbage In, Garbage Out."  But, I'll leave the business decisions to you/your company.

2) Use Google API's.  Their Canonical Address API lets you enter any address, and it will return if it knows this address, and the correct way to review it, including things like converting "ln" to "lane" or ", 3C" to ", Unit 3C"  
 - Then you can take the data and insert it as you see fit.  Since the address format will be stable, you can depend on your parsing code to operate well.
 - I believe the free version is limited to about 1000 addresses a day, but you can use that to prove your app works, and then pay a small fee to open it up for a bulk transmission to convert everything.  
 - Also, you can flag anything Google didn't recognize, which usually is low, and deal with those ones manually, Often its a typo converting "Lane" to "Street" for instance, but the house number only exists on Lane.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 38721274
Try this formula

WhilePrintingRecords;
Global StringVar Array AddressParts;
Global StringVar StreetNumber;
Global StringVar StreetName;
Global StringVar UnitNumber;
Local NumberVar Index;

If Not IsNull(YourField) then
(
    AddressParts := Split({YourField}," ");
    StreetNumber := AddressParts[1];
    If UBound(AddressParts] >= 2 then
        StreetName := Replace(AddressParts[2],",","");
    If UBound(AddressParts] >= 3 then
        For Index := 3 to UBound(AddressParts) do
             UnitNumber := UnitNumber & " " & AddressParts[Index];
)
""

Open in new window


To display them in the report use 1 formula per part
WhilePrintingRecords;
Global StringVar StreetNumber;
StreetNumber

Open in new window


Similar formulas for the other 2 parts

mlmcc
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ISBTECH
ID: 38721303
@ mlmcc, I paste that into a formula and I get the following error:
12-26-2012-10-03-09-AM.png
0
 
LVL 100

Accepted Solution

by:
mlmcc earned 500 total points
ID: 38722369
Change the ] to a )

It has the same problem a couple of lines later

WhilePrintingRecords;
Global StringVar Array AddressParts;
Global StringVar StreetNumber;
Global StringVar StreetName;
Global StringVar UnitNumber;
Local NumberVar Index;

If Not IsNull(YourField) then
(
    AddressParts := Split({YourField}," ");
    StreetNumber := AddressParts[1];
    If UBound(AddressParts) >= 2 then
        StreetName := Replace(AddressParts[2],",","");
    If UBound(AddressParts) >= 3 then
        For Index := 3 to UBound(AddressParts) do
             UnitNumber := UnitNumber & " " & AddressParts[Index];
)
"" 

Open in new window

0
 

Author Closing Comment

by:ISBTECH
ID: 38723758
Amazing as always!  Thanks!
0

Featured Post

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.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

19 Experts available now in Live!

Get 1:1 Help Now