?
Solved

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

Posted on 2009-07-15
7
Medium Priority
?
508 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:mikespiegel99
[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
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 24864197
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
 

Author Comment

by:mikespiegel99
ID: 24864631
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
 
LVL 42

Expert Comment

by:pcelba
ID: 24864831
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:mikespiegel99
ID: 24869023
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
 
LVL 42

Expert Comment

by:pcelba
ID: 24869582
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
 
LVL 42

Accepted Solution

by:
pcelba earned 1000 total points
ID: 24869607
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
 

Author Closing Comment

by:mikespiegel99
ID: 31603985
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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

777 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