July 19, 2006 at 10:54 am
Can you join two tables or views using LIKE as the join operator as in:
Select x, y, z FROM TableA JOIN TableB ON TableA.Name LIKE TableB.Name
SQL Server accepts the SQL, but the query returns no results when I know there are records that would match the LIKE.
Background:
I have two tables with Company Names. Unfortunately they came from different sources so they're not formatted the same way. For example:
TableA.Name: XYZ, Inc TableB.Name: XYZ Incorporated.
The names in TableA are invariably shorter than those in TableB so I figured "Make a view that computes a new name field with all the spaces and punctuation squeezed out, add a '%' to the name in TableA and you can use LIKE to join the tables." However, it ain't working. I have:
ViewA.Name2: XYZINC% ViewB.Name2: XYZINCORPORATED
So if LIKE is valid then "Select * from ViewA as A JOIN ViewB as B ON A.Name2 LIKE B.Name2" should work. No such luck.
If LIKE is not supported in a JOIN does anyone have an idea how to merge the data to a new table using a set-based approach? I've been playing with subqueries to create a single table with the merged data as an alternative approach but so far no luck...
July 19, 2006 at 11:02 am
try
Select A.*,B.* from ViewA as A JOIN ViewB as B
ON B.Name2 LIKE A.Name2
Vasc
July 19, 2006 at 11:23 am
That's basically what I've been doing with no luck. Instead of theory here's the real stuff:
Sample data from vw_CRM_Accounts:
[Company Name] DAM Squeezed
A O Smith | Lanphere Wendy | AOSMITH% |
A O Smith | Lanphere Wendy | AOSMITH% |
AARP | Biles Mike | AARP% |
AAS/Chaas Acquisitions | Saunders Carolyn | AASCHAASACQUISITIONS% |
ABX Air Inc | Vanden Heuvel Cheryl | ABXAIRINC% |
ABX Air Inc | Vanden Heuvel Cheryl | ABXAIRINC% |
Acco/Wilson | NULL | ACCOWILSON% |
ACCURIDE CORPORATION: | Blanton Cindy | ACCURIDECORPORATION:% |
ACF Industries, Inc. | McCallum Mike | ACFINDUSTRIESINC% |
Sample data from vw_CDW_Accounts:
Acct_No Acct_Name Squeezed
42378 A A A /AMERICAN AUTOMOBILE ASSOC E AAAAMERICANAUTOMOBILEASSOCE
77216 A A R P E AARPE
77213 A A R P / FOUNDATION GRANTS / SCSEP E AARPFOUNDATIONGRANTSSCSEPE
42475 A C F INDUSTRIES INC E ACFINDUSTRIESINCE
47156 A D P TOTALSOURCE INC. W ADPTOTALSOURCEINCW
77203 A M R CORPORATION S AMRCORPORATIONS
78008 A S C INC C ASCINCC
77272 A T & T E ATTE
42606 A T & T ET AL E ATTETALE
Sorry for the small print but the CDW names are much longer...
Here's the query:
A.[Company Name], A.DAM, B.Acct_No, B.Acct_Name
vw_CRM_Accounts as A JOIN vw_CDW_Accounts as B
A.Squeezed LIKE B.Squeezed
B.Acct_Name LIKE 'A%'
I limited it to accounts starting with "A" just to keep the run time short. THey're both large views.
Unless I'm missing something I would expect to get matches "AARP%" LIKE "AARPE" and "AARPFOUNDATION...". I get no rows returned.
July 19, 2006 at 11:35 am
declare @a table(name varchar(50))
declare @b-2 table(name varchar(50))
insert into @a
select 'AOSMITH%' UNION ALL
select 'AARP%' UNION ALL
select 'AASCHAASACQUISITIONS%' UNION ALL
select 'ABXAIRINC%' UNION ALL
select 'ACCOWILSON%' UNION ALL
select 'ACCURIDECORPORATION:%' UNION ALL
select 'ACFINDUSTRIESINC%'
insert into @b-2
select 'AAAAMERICANAUTOMOBILEASSOCE' UNION ALL
select 'AARPE' UNION ALL
select 'AARPFOUNDATIONGRANTSSCSEPE' UNION ALL
select 'ACFINDUSTRIESINCE' UNION ALL
select 'ADPTOTALSOURCEINCW' UNION ALL
select 'AMRCORPORATIONS' UNION ALL
select 'ASCINCC' UNION ALL
select 'ATTE' UNION ALL
select 'ATTETALE'
select a.name,b.name from @a a join @b-2 b on
b.name like a.name
as posted before try:
Vasc
July 19, 2006 at 12:12 pm
Duh..
Thanks. That worked.
July 19, 2006 at 12:31 pm
Related question:
How do I get the rows in B that are not matched by a row in A?
July 19, 2006 at 12:39 pm
Distinct A.[Company Name], A.DAM, B.Acct_Number, B.Acct_Name,
.Squeezed, B.Squeezed
vw_CRM_Accounts as A right JOIN vw_CDW_Accounts as B
B.Squeezed LIKE A.Squeezed
I knew there was an answer buried in there somewhere...
July 19, 2006 at 12:41 pm
Ooops...
gives me rows in B with no match in A...
July 20, 2006 at 10:33 am
If you do a join using LIKE without using any wildcards, it is the same as doing the join using an = instead of LIKE ..... only slower. So, all you have achieved is making it slower.
Do it like this:
July 20, 2006 at 11:05 am
The value of A.Squeezed already contains the wildcard (see examples above).
July 20, 2006 at 11:11 am
You don't need to create a view with the wildcards built in. You can do it like I did above. Using a view like this would be comparable to pushing in a thumbtack with a sledgehammer.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply