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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 @parameter question
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

120 Posts

Posted - 05/06/2013 :  12:36:35  Show Profile  Reply with Quote
I have a field that has multiple values I would like to select. it contains 10,20,21,23,90. I am trying to be able to write this out not using a stored procedure. I tried using LIKE and IN but I can only use 1 division at a time.

WHERE ([div-code] LIKE @division)

End result I would like to get is @division 10, 23 <enter>

Data shows me all records that are from Division 10 and 23.

Flowing Fount of Yak Knowledge

1054 Posts

Posted - 05/06/2013 :  12:45:33  Show Profile  Reply with Quote
First of all IN is translated as OR e.g. in(10,23), means 10 or 23, not and.

Two ways to do so
1)not an optimized approach
WHERE ','+@division+',' like ','+[Div-Code]+',' -- If Div-Code is not varchar then you'll need to convert it to varchar e.g. convert(varchar,[Div-Code])

2)an Optimize approach
Declare/Create a (temporary) table and convert the CSV parameter value into tabular format. then simply join the temporary table with the actual table based on respective columns

Go to Top of Page

Posting Yak Master

120 Posts

Posted - 05/06/2013 :  14:50:26  Show Profile  Reply with Quote
Thank you!
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 05/07/2013 :  00:37:05  Show Profile  Reply with Quote
another way is this


SQL Server MVP
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.09 seconds. Powered By: Snitz Forums 2000