views:

86

answers:

2

I have two tables, I want to search TermID in Table-A through TermID in Table-B and If there is a termID like in Table-A and then want to get result table as shown below.
TermIDs are in different length. There is no search pattern to search with "like %"
TermIDs in Table-A are part of the TermIDs in Table-B
Regards,

Table-A

ID         TermID
101256666 126006230
101256586 126006231
101256810 126006233
101256841 126006238
101256818 126006239
101256734 1190226408
101256809 1190226409
101256585 1200096999
101256724 1200096997
101256748 1200097005

Table-B

TermNo TermID
14 8990010901190226366F
16 8990010901190226374F
15 8990010901190226382F
18 8990010901190226408F
19 8990010901190226416F
11 8990010901200096981F
10 8990010901200096999F
12 8990010901200097005F
13 8990010901200097013F
17 8990010901260062337F

As a result I want to get this table;
Result Table -TableA.ID TableA.TermID TableB.TermNo

A.ID      A.TermID  B.TermNo
101256734 1190226408 18
101256585 1200096999 10
101256748 1200097005 12
A: 

Why wouldn't something like:

select a.id
       a.termid
       b.termno
  from Table-A A
       Table-B B
 where B.TermID LIKE '%'+A.TermID+'%'

work?

Mark Baker
A: 

Your expected resultset is wrong: TABLE_A.TERMID = 126006233 matches TABLE_B.TERMID = 8990010901260062337F.

Anyway here is a solution:

SQL> select a.id, a.termid, b.termno
  2  from table_a a cross join table_b b
  3  where instr(b.termid, a.termid) != 0
  4  order by b.termno
  5  /

        ID TERMID                   TERMNO
---------- -------------------- ----------
 101256585 1200096999                   10
 101256748 1200097005                   12
 101256810 126006233                    17
 101256734 1190226408                   18

SQL>

edit

I suppose I had better point out that INSTR() is an Oracle function. MySQL also has it. But if by [sql] you meant "SQL Server" then you should substite the CHARINDEX() function.

APC