I have 2 tables like this,
Table1
Id Locations
-- ---------
1 India, Australia
2 US , UK
Table2
Table2Id Location
-------- --------
101 Italy
102 UK
103 Hungary
104 India
I need to inner join these 2 tables on the condition, If Locations
in table2 contains Location
field in table1. The result will be like
Id Table2Id Location Locations
-- -------- -------- ---------
1 104 India India, Australia
2 102 UK US , UK
I tried something like
Select t1.id,
t2.Table2Id,
t1.Locations,
t2.Location
From Table1 t1
Inner join Table2 t2 On CONTAINS(t1.Locations, t2.Location)
But the second parameter of contains
should be a string. Its not allowing to give the column name there.
I cannot use temptable
or variable
in the query. Because this query needs to be run on a email campaign tool called ExactTarget
where there is no support for temptable
and variables
.
Any help will be highly appreciated. Thank you.
',' + replace(table1.locations,', ', ',') + ',' like '%,' + table2.location + ',%'
in the join might look ugly but may do the trick – Symbolism