I am stuck in joining two tables "a" and "b". The structure of the two tables is given 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 +
+-------+---------------------+
Table SKU convention is ParentSKU + "" + Child SKU. So each sku in table has a child sku. Parent child is saperated by "".
I wan to Join table A.SKU part after "_" on table B.SKU
So far I have tried the following query but without 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 highly appreciated.