Solved

stripping data from a Varchar feild in Crystal 2008

Posted on 2012-12-26
6
248 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 36
Get the latest status 8 32
SQL - Update field defined as Text 6 17
Loops and updating in SQL Query 9 29
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

809 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