Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parsing address using tsql

Posted on 2014-01-23
5
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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