Find Like values in two columns in the same table SQL 2005

I'll expand on what I'm trying to accomplish.  

I have a table called MASTER that contains names and addresses

I have a table that contains addresses that I received from a batch scrubbing
service that can provide me with new addresses at times and the same address
on file at times called SERVICES_CT_ADDRESS

I want to take the address in the SERVICES_CT_ADDRESS table and compare it
against the address in the MASTER table and if the address is different, I
want to update the address in the MASTER table with the address in the
SERVICES_CT_ADDRESS table.  I know how to perform the update for the address,
but I just can't figure out how to be able to compare the two addresses
against each other so I can remove the ones that match.

The address in the MASTER table is stored in these fields:  STREET1, STREET2,
CITY, STATE, ZIPCODE

The address in the SERVICES_CT_ADDRESS table is stored in these fields:
PRIMARYSTREETID, PREDIRECTION, STREETNAME, POSTDIRECTION, STREETSUFFIX,
UNITTYPE, UNITID, CITY, STATE, ZIPCODE   This is why I have to perform the
CONCATENATE in order to combine the street address elements to match the
format of the STREET1 and STREET2 fields in the MASTER table.

This is an example of what the data looks like:  (The STREET1 address is the
address from the SERVICES_CT_ADDRESS table, and the OLDSTREET1 address is the
address from the MASTER table.   You'll notice the extra spaces in the
addresses listed under STREET1

STREET1                                  OLDSTREET1
601  MARTINIQUE  AVE             2804 S LLEWELLYN AVE
1107  FULCHER  LN                   1107 FULCHER LN
1813  LINDEN  ST                       4736 N LOTUS AVE
735                                            735 FRANKLIN RD

Note, the last record listed "735 Franklin Rd"  I manually updated the
STREET1 value to "735" just to see if the query below would return that
record, and it did not.  It leads me to beleive that the issue is not with
the extra spaces in the STREET1 field.  Do you have any suggestions?

SELECT STREET1, OLDSTREET1
FROM SRA_TRIGGER_ADDRESS
WHERE STREET1 LIKE OLDSTREET1

Thanks,

Mike Spiegel
mikespiegel99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
The LIKE operator requires "string pattern" for approximate comparison. String pattern contains % ( means any number of characters) and _  (means one character of any value). So you may compare:

STREET1 LIKE '735%'
or
STREET1 LIKE '%FULCHER%'
etc.

To decide if two addresses are equal in your data is much more complex. BUT if you are planning to replace the Master address by the new one if they are different then you may replace the address always and you don't need to compare them. Or you may check the exact match only.

More important is to keep address history and change date.

0
mikespiegel99Author Commented:
pcelba,

Unfortunatley that won't work for me.  This is going to be part of an automated process affecting hundreds of records so I won't be eyeballing the process.  Also, I can't just overwrite the address if it matches or is close to matching because I am sending out a letter to the new addresses that I'm importing from the SERVICES_CT_ADDRESS table.  So, I have to make sure that I'm updating the MASTER table with a new address.  Do you have any suggestions?
0
pcelbaCommented:
Automated process which validates manual data is always a bet... You are never sure which data are correct. Even newer data can contain new typo bugs.

If you need just the closest match then you have to remove all duplicate spaces and compare two strings. You could maybe compare strings converted to upper case. Everything else must be handled as address change. Even the change of "Rd" to "Way" or N to S can be important.

First you have to define what means "address change" and "address unchanged" then switch this definition to the algorithm (means to create user function for data conversion), create temporary data containing columns to match and use them in query.

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mikespiegel99Author Commented:
pcelba,

Yes, that's what I'm trying to accomplish, but I don't know how to remove the duplicate spaces.  Do you know how to do that?

Thank you
0
pcelbaCommented:
You may define user function to remove duplicate spaces and use it everywhere when needed:

select dbo.MyFceRemoveDupSpaces('   fdsfd fdsfds sfd    sfdsfs erwqre e234723 brws  ')



CREATE FUNCTION dbo.MyFceRemoveDupSpaces (@InputStr varchar(1000))
   RETURNS varchar(1000)
AS
BEGIN
WHILE CHARINDEX('  ',@InputStr) > 0
  SET @InputStr = REPLACE(@InputStr, '  ', ' ')
RETURN @InputStr
END

Open in new window

0
pcelbaCommented:
To remove duplicate spaces from existing data you may use it in UPDATE statement:

UPDATE SRA_TRIGGER_ADDRESS SET STREET1 = dbo.MyFceRemoveDupSpaces(STREET1)
   WHERE CHARINDEX('  ',STREET1) > 0

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikespiegel99Author Commented:
pcelba,

That solved my problem!  I was able to remove the similar addresses using the query below.  Now all I have to do is some more QC and I'm golden!

Thanks!

DELETE
FROM SRA_TRIGGER_ADDRESS
WHERE LEFT(OLDSTREET1, 7) LIKE LEFT(STREET1, 7)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.