vivek
vivek

Reputation: 3

sql join issues with different data

I want to join two tables with different data and data type is same.

In tableA the column col1 is with varchar datatype i.e. 123 and in tableB the column col1 is with varchar datatype i.e. ABC-123

Is there any way to join both columns by adding ABC as prefix to col1 in table 1 or by removing prefix ABC from col1 table 2.

enter image description here

Upvotes: 0

Views: 51

Answers (2)

The Impaler
The Impaler

Reputation: 48865

You can use CONCAT(), as in:

select *
from table_a a
join table_b b on concat('ABC-', a.col1) = b.col2

This issue is quite common, specially in old databases, where you need to join VARCHAR columns with NUMERIC ones, since the designers back in the 90s though it that way.

Upvotes: 3

Grace
Grace

Reputation: 888

Just use function CONCAT() at the ON clause of INNER JOIN

select * from
tableA a inner join tableB b
on CONCAT('ABC-', a.col1) = b.col2

Upvotes: 3

Related Questions