Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 WHERE Field in (@Var)

Author  Topic 

12many
Starting Member

9 Posts

Posted - 2013-03-08 : 12:46:23
Hi there

I have a field in table one that is a delimited list of codes(a,b,c,d) that relate to data in table 2

I have created a @VAR in in my report that reads


DECLARE @VAR VARCHAR(50)
SET @VAR '''' + REPLACE((SELECT fields FROM table1),',',''',''') + ''''

So the Value in @Var will read 'a','b','c','d'

What i was then hoping to be able to do is

SELECT Data FROM Table2 WHERE Code IN (@VAR)

It has come to my attention this is not possible

Can any one give any advise on how this can be achieved

Ian

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-08 : 13:09:34
you need to use a string parsing method

see an example below

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

12many
Starting Member

9 Posts

Posted - 2013-03-14 : 06:46:59
Hi visakh16

Thanks Alot for this, Ill give it a bash when i get a chance,

Ian
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-03-14 : 09:28:29
why not just store the values in table1 properly (i.e. 1 per row) then simply use
SELECT Data FROM Table2 WHERE Code IN (select field from table1)
?
Go to Top of Page
   

- Advertisement -