Solved

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

Posted on 2009-07-15
7
494 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
  • 4
  • 3
7 Comments
 
LVL 41

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 41

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 41

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 41

Accepted Solution

by:
pcelba earned 250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now