Question : How to make IN operator to accept CSV which are varchar in SQL Server 2008

I have the following tables

tbl_Countries
==========
CountryID    Name
---------------------
1                  USA
2                  Australia
3                  Canada

tbl_Users
=========
UserName    AllowedContries
--------------------------
A                  1,3
B                   1
C                    2,3

I am trying to get the output like

A                USA,Canada
B                USA
C                Australia, Canada

I want to achive this using sql query not Function or procedure.

I am using the following query

select *,
(SELECT STUFF((SELECT ',' + Name FROM tbl_Countries where tbl_Countries.CountryId in (tbl_Users.AllowedCOuntries)  FOR XML PATH('')),1,1,'')) AS CSV
from tbl_Users

Getting the following error

Conversion failed when converting the varchar value '209,122' to data type int.


Answer : How to make IN operator to accept CSV which are varchar in SQL Server 2008

please try this:
1:
2:
3:
4:
5:
6:
select *,
(SELECT STUFF((SELECT ',' + Name FROM tbl_Countries 
   where ',' + tbl_Users.AllowedCOuntries + ',' Like '%,' + cast(tbl_Countries.CountryId as varchar(20)) + ',%'  
  FOR XML PATH('')),1,1,'')) AS CSV
from tbl_Users
Random Solutions  
 
programming4us programming4us