I am working on SQL Server 2008 R2.
The following query:
select alloted_area
from tbllogin
where nid = 82
returns this
alloted_area
1,101,1002,79,365,297
The following query
select *
from tblarea
where nid in (1, 101, 1002, 79, 365, 297)
returns this
nid area_name
------------------
1 RAJASTHAN
101 RAJASTHAN
79 RAJASTHAN
297 RAJASTHAN
365 RAJASTHAN
1002 RAJASTHAN
But the following query
select *
from tblarea
where nid in (select alloted_area from tbllogin where nid = 82)
generates this error:
Conversion failed when converting the varchar value '1,101,1002,79,365,297' to data type int
What should I do?
Perhaps I have to make rows out of alloted_areas in tbllogin on the basis of comma sign. How do I do that?
As suggested in the comments I did try not to store data as comma separated values but that would mean creating an entire table to store that information. So is there any way I can achieve the desired result in my situation.
INis working properly. What you are doing is the equivalent ofnid = cast( '1,101,1002,79,365,297' as int), which of course doesn't work.