Johnny Bigoode
Johnny Bigoode

Reputation: 591

How can I JOIN two tables columns using a wildcard?

I'm using Access 97.

This is what I wanna do:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber LIKE ListOfProducts.IDNumber*;

Sometimes the IDNumber on ListOfProducts has an extra letter indicating some information about the product.

So I have the IDNumber AC244 on CodeDIS but I have AC244P on ListOfProducts.

All I want is to add a wildcard to the end of the condition.

Is this possible?

Upvotes: 1

Views: 2878

Answers (3)

Roger Dodger
Roger Dodger

Reputation: 11

I know this is old, but I have not posted much if ever and thought I would try here.

Why not use a substring function with PATINDEX testing if the last digit is always a character? I'm sure my code is mega-inefficient, but here goes my beginner attempt. I'll bet some could golf my case statement into something smaller.

-- I used the code below
-- It only works if the final digit is lower or uppercase letter of 
-- specified language.
DECLARE @CodeDIS TABLE (IdNumber nvarchar(50), BaseRowNum nvarchar(50));

insert into @CodeDIS (IdNumber,BaseRowNum) values ('52352345','1')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('134131','2')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343141','3')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a143321','4')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('c34324','5')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343214','6')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('%134324','7')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a%134324','8')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1413','9')
;
DECLARE @ListOfProducts TABLE (IdNumber nvarchar(50), UpdateSourceDataColumn nvarchar(50));

insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('52352345a','11')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('134131','22')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343141a','33')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a143321','44')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('c343245','55')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343214Z','66')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('%134324','77')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a%134324','88')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1413\','99')
;
SELECT a.*,b.IdNumber as IdNumber2, b.UpdateSourceDataColumn
    FROM @CodeDIS a 
    left join @ListOfProducts b
    on a.IdNumber = 
      case 
        when PATINDEX(
          '[a-zA-Z]', 
          substring(b.IdNumber,len(b.IdNumber),1) 
          COLLATE Latin1_General_CS_AS
        )=1
        then left(b.IdNumber,len(b.IdNumber)-1)
        else b.IdNumber
      end
    ;     
--IdNumber  BaseRowNum  IdNumber2   UpdateSourceDataColumn
--52352345  1           52352345a   11
--134131    2           134131      22
--1343141   3           1343141a    33
--a143321   4           a143321     44
--c34324    5           NULL        NULL
--1343214   6           1343214Z    66
--%134324   7           %134324     77
--a%134324  8           a%134324    88
--1413      9           NULL        NULL

Upvotes: 1

mikeY
mikeY

Reputation: 529

How about:

SELECT * 
FROM CodeDIS, ListOfProducts 
WHERE CodeDIS.IDNumber LIKE left(ListOfProducts.IDNumber,5); 

or if you like:

SELECT * 
FROM CodeDIS a inner join ListOfProducts b on a.IDNumber = left(b.IDNumber,5); 

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91376

If the choice is C or P, why not:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber = ListOfProducts.IDNumber & "C"
OR CodeDIS.IDNumber = ListOfProducts.IDNumber & "P"

Upvotes: 1

Related Questions