SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 WHERE Field in (@Var)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

12many
Starting Member

9 Posts

Posted - 03/08/2013 :  12:46:23  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 03/08/2013 :  13:09:34  Show Profile  Reply with Quote
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 - 03/14/2013 :  06:46:59  Show Profile  Reply with Quote
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 - 03/14/2013 :  09:28:29  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.34 seconds. Powered By: Snitz Forums 2000