I have two tables and a fairly complicated SQL query to extract data from these tables - this all works fine until it comes across a value within a column which has more than one id - comma separated. So to simplify the area I'm struggling with, let's assume the following
Table 1 (T1)
ID First Name Last Name Active
--------------------------------------------
101 Fred Bloggs 1
102 John Smith 0
103 Elizabeth Dawson 1
104 Amy Johnson 1
Table 2 (T2)
ID Postcode HouseNo
-----------------------------------
101 TS15 9AZ 42
102 TQ1 4TF 3
103, 104 WA1 4AA 7
So assuming I want to return results of who lives at what address, I join the tables on ID and write a fairly simple query like
select
T1.FirstName + ' ' + T1.Lastname as fullname, T2.Postcode, T2.HouseNo
from
T1
join
t2 on t1.id = t2.id
where
t1.active = 1
This query works fine until it comes across the comma separated value when it returns the error:
Conversion failed when converting the varchar value '103,104' to data type int
What it should return is
Fullname PostCode HouseNo
-------------------------------------------------------
Fred Blogs TS15 9AZ 42
Elizabeth Dawson Amy Johnson TQ1 4TF 3
Any ideas on how to make this work?