I am stuck in joining two tables "a" and "b". The structure of these two tables is shown below.
Table a
+-------------+---------------------+
+ SKU | Title +
+-------------+---------------------+
+ 12345_786 | Some text +
+ | +
+ 12345_231 | Sony +
+ | +
+ 12345_222 | Samsung +
+ | +
+ 67481_21 | IBM +
+ | +
+ 88723_231 | HP +
+-------------+---------------------+
Table B
+-------+---------------------+
+ SKU | Price +
+-------+---------------------+
+ 786 | $230 +
+ | +
+ 231 | $540 +
+ | +
+ 222 | $120 +
+ | +
+ 21 | $220 +
+ | +
+ 231 | $50 +
+-------+---------------------+
The SKU table is ParentSKU + "+ Child SKU. Thus, each sku in the table has a child sku. The parent child is smoothed by the letter" ".
I want to join table A.SKU after "_" in table B.SKU
So far I have tried the following query, but without the desired result.
SELECT A.SKU,B.Price
FROM A
INNER JOIN
B ON
Substring(A.SKU, patindex('%_%', A.SKU),
Cast(Len(A.SKU) as int)-cast(patindex('%_%',A.SKU)as int))
= CAST(B.SKU AS varchar(12))
Your help in this context will be greatly appreciated.
source
share