I have 2 simplified tables (all columns are varchar). Some rows in T1_TAB for F2 contain multiple values separated by ;, some do not have separators at all as shown below (sometimes ; might also appear at the beginning and/or at the end). F2 in T2_TAB always has a single value.
I need to be able to pull rows from ether table based on single selection from one table and likeliness on F2 columns.
T1_TAB
F0 | F2
--------------
1 ;30
2 ;10;20;30
3 ;20;30;
4 10
T2_TAB
F1 | F2
-------------
100 10
200 20
300 30
I can do:
SELECT T1.F0
FROM T1_TAB T1
LEFT JOIN T2_TAB T2
ON T2.F2 LIKE '%' + T1.F2 + '%'
WHERE T2.F1 = '200'
This would bare result:
2
3
Now, I need to do the opposite. For instance:
Based on condition WHERE T1.F0 = 3, I need to pull from T2 rows with F1 equals 200 and 300 respectively. I guess I need to somehow split ;20;30; by a “;” and do the loop to match each value separately at run-time disregarding blank tokens.
You can create a function which converts the semicolon-separated string into a table of values:
and then