Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parsing address using tsql

Posted on 2014-01-23
5
Medium Priority
?
338 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 2000 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

885 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