Alex Alex - 3 months ago 44
SQL Question

Select from two tables joined by different number of columns

I have two tables. In first, I have a column which holds an alphanumeric with this format: X12345678A (1 letter, 8 numbers and 1 letter).

In second table, I have the same field, but divided into three columns, the first containing the leading letter, the second containing the number, and the third containing the trailing letter.

My question: if I can't modify the table structure, how can I join both tables efficiently, ie, using an index?

Thank you in advance!


With string concatenation:

select . . .
from t1 join
     on t1.col = t2.col1 || t2.col2 || t2.col3;

For an efficient join, you can try an index on t1(col) or an index on the expression t2(col1 || col2 || col3).