Solved

stripping data from a Varchar feild in Crystal 2008

Posted on 2012-12-26
6
253 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to keep a record with the highest value 3 38
disk usage reporting tools 27 50
Database Mail Profiles 1 18
SQL 2012 Instance Problem 3 33
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

735 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