I didn't find much info on inner joins with substring.
I am not very well versed in SQL and I am trying to do a string match here but am getting a problem with the LIKE
operator in the INNER JOIN
clause.
I have data in Table 1 and Table 2. Table 1 for example has JUY and Table 2 has Tyy_ss_JUY. Both the tables have over 10000 entires. I want to match both and give me a result when it matches the string.
Assume that I have two tables as follows:
Table1 LocationID Model CAMERA 1 Zone A ABCD 2 Zone B ALI 3 Zone A JUY 4 Zone A LOS 5 Zone C OMG
Table2 Vehicle NAME Honda Txx_ss_ABCD Myvi Tyy_ss_ABCD Vios Tyy_ss_JUY Proton Tyy_ss_LOS SUV Tyb_ss_OMG SUV UUS_ss_OMG SUV Lyx_ss_JUY
SELECT Vehicle,NAME
FROM Table2
INNER JOIN (SELECT CAMERA FROM Table1 WHERE Model LIKE '%Zone A%')sub on
NAME LIKE '%'+sub.CAMERA+'%'
Expected Result
Result Vehicle NAME Honda Txx_ss_ABCD Myvi Tyy_ss_ABCD Vios Tyy_ss_JUY Proton Tyy_ss_LOS SUV Lyx_ss_JUY
I get an error message in DB2 when I execute this
Invalid character found in a character string argument of the function "DECFLOAT".. SQLCODE=-420, SQLSTATE=22018, DRIVER=3.69.24 SQL Code: -420, SQL State: 22018
Thank you
DB2 doesn't support the '+' symbol for string concatenation.
Use one of the following ways to get the desired result instead:
with
Table1(LocationID, Model, CAMERA) as (values
(1, 'Zone A', 'ABCD')
, (2, 'Zone B', 'ALI')
, (3, 'Zone A', 'JUY')
, (4, 'Zone A', 'LOS')
, (5, 'Zone C', 'OMG')
)
, Table2 (Vehicle, NAME) as (values
('Honda', 'Txx_ss_ABCD')
, ('Myvi', 'Tyy_ss_ABCD')
, ('Vios', 'Tyy_ss_JUY')
, ('Proton', 'Tyy_ss_LOS')
, ('SUV', 'Tyb_ss_OMG')
, ('SUV', 'UUS_ss_OMG')
, ('SUV', 'Lyx_ss_JUY')
)
SELECT Vehicle,NAME
FROM Table2
INNER JOIN (SELECT CAMERA FROM Table1 WHERE Model LIKE '%Zone A%')sub on
NAME LIKE
'%'||sub.CAMERA||'%'
--concat(concat('%', sub.CAMERA), '%')
;