Solved

parsing address using tsql

Posted on 2014-01-23
5
328 Views
Last Modified: 2014-02-05
I have a legacy table with input in the address field like this:

CITY OF ACWORTH TAX DEPA -4415 SENATOR RUSSELL
LUCERNE VILLAGE TAX COL -2073 MAIN RD, STE A
BROCKTON CITY (W/S)- COL 45 SCHOOL STREET
YORK COUNTYTREASURER-28 EAST MARKET ST. -
EASTLAND CAD - TAX COLLE P O BOX 914
BLOOMFIELD VILLAGE TREAS P.O. BOX 609
ABBEVILLE CITY - TAX COL P.O. BOX 1170
ABBEVILLE COUNTY - TREAS P O BOX 38
ABBEVILLE CITY - TREASU P O BOX 40

I need to parse out this field to give me only the address. I am hitting a dead end. Help please!
0
Comment
Question by:Leogal
  • 2
  • 2
5 Comments
 

Author Comment

by:Leogal
ID: 39805006
Here is the code I am using at this point. It is not totally doing what I want tho...

   , CASE
              WHEN  [PAddress] > ' ' THEN
                           SUBSTRING( [PAddress],charindex( ' - ', ([PAddress]))  , 25)
                              ELSE ' '
               END AS [addr1]


here are the results , close but not exactly right as I only need the address....

CITY OF ACWORTH TAX DEPA
LUCERNE VILLAGE TAX COL
BROCKTON CITY (W/S)- COL
YORK COUNTYTREASURER-28
 - TAX COLLE P O BOX 914
BLOOMFIELD VILLAGE TREAS
 - TAX COL P.O. BOX 1170
 - TREAS P O BOX 38
 - TREASU P O BOX 40
 - COLLE 269 HIGH ST
 - TAX COLL 60 N PARKE ST
 - TREASURE P O BOX 789
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39805404
Perhaps something like this:
, CASE 
	WHEN LEN(PAddress) > 0 AND CHARINDEX('-', (PAddress)) > 0 THEN SUBSTRING([PAddress], CHARINDEX('-', (PAddress)) + 1, 25)
	ELSE ' '
  END AS [addr1]

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 500 total points
ID: 39805406
This is how I tested it:
DECLARE @YourTable TABLE(
		PAddress varchar(100))

INSERT @YourTable(PAddress)
VALUES  ('CITY OF ACWORTH TAX DEPA -4415 SENATOR RUSSELL'),
	('LUCERNE VILLAGE TAX COL -2073 MAIN RD, STE A'),
	('BROCKTON CITY (W/S)- COL 45 SCHOOL STREET'),
	('YORK COUNTYTREASURER-28 EAST MARKET ST. -'),
	('EASTLAND CAD - TAX COLLE P O BOX 914'),
	('BLOOMFIELD VILLAGE TREAS P.O. BOX 609'),
	('ABBEVILLE CITY - TAX COL P.O. BOX 1170'),
	('ABBEVILLE COUNTY - TREAS P O BOX 38'),
	('ABBEVILLE CITY - TREASU P O BOX 40')

SELECT  CASE 
		WHEN LEN(PAddress) > 0 AND CHARINDEX('-', (PAddress)) > 0 THEN SUBSTRING([PAddress], CHARINDEX('-', (PAddress)) + 1, 25)
		ELSE ' '
        END AS [addr1]
FROM    @YourTable

Open in new window

Here is the output:
4415 SENATOR RUSSELL
2073 MAIN RD, STE A
 COL 45 SCHOOL STREET
28 EAST MARKET ST. -
 TAX COLLE P O BOX 914
 
 TAX COL P.O. BOX 1170
 TREAS P O BOX 38
 TREASU P O BOX 40
0
 
LVL 32

Expert Comment

by:awking00
ID: 39806859
>>I have a legacy table with input in the address field like this:
CITY OF ACWORTH TAX DEPA -4415 SENATOR RUSSELL
LUCERNE VILLAGE TAX COL -2073 MAIN RD, STE A
BROCKTON CITY (W/S)- COL 45 SCHOOL STREET
YORK COUNTYTREASURER-28 EAST MARKET ST. -
EASTLAND CAD - TAX COLLE P O BOX 914
BLOOMFIELD VILLAGE TREAS P.O. BOX 609
ABBEVILLE CITY - TAX COL P.O. BOX 1170
ABBEVILLE COUNTY - TREAS P O BOX 38
ABBEVILLE CITY - TREASU P O BOX 40<<
So what do you want to see as the addresses from these?
0
 

Author Comment

by:Leogal
ID: 39837902
I wanted to be able to parse the address field showing only the street address or post office box.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 48
T-SQL: Do I need CLUSTERED here? 13 40
SQL Error - Query 6 24
Increment column based of a FK 8 20
In this article I will describe the Backup & Restore 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

786 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