0

I have 2 tables that I need to match. Please see example.

TB1

ID | String1 
1  | Apt 2
2  | Apt 23
3  | Apt 22

TB2

ID2| String2
1  | 23 Apartment
2  | 22 The Block
3  | 2 Complex

I need to find which ID's Match from TB1 to TB2 WHERE the number part of string 1 to string2 match

OUTPUT

 ID | Matching ID2 | String1 | String 2
 1  | 3            | Apt 2   | 2 Complex
 2  | 1            | Apt 23  | 23 Apartment
 3  | 2            | Apt 22  | 22 Apartment
Matt Dykstra
  • 55
  • 2
  • 8

2 Answers2

0

IMHO this is bad table design. If you have to do such matches, store the number in a separate field.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

If you really really can't change the table format then I would create a function to strip all of the non-numeric characters from a given string and then use it in your query.

Luckily, this is a task that has been tackled before (hope your Italian is up to scratch)

You could then use the function in your query.

Hence:

select a.ID, b.ID as "Matching ID2",a.String1,b.String2    
from TB1 as a INNER JOIN TB2 as b    
ON LeaveNumber(a.String1) = LeaveNumber(b.String2)
ORDER BY a.ID asc;

Not fool proof and probably not the fastest but it should get you what you want.

Though I would seriously consider altering this table design if you have the chance. Though it sounds as though it is either CSV engine table or populated directly from a CSV in which case you probably don't have too much control over it!

Community
  • 1
  • 1
Tom Mac
  • 9,693
  • 3
  • 25
  • 35