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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

hbadministrator
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.

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
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

Cheers
MIK
Go to Top of Page

hbadministrator
Posting Yak Master

120 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

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

http://visakhm.blogspot.in/2013/01/string-pattern-search-xml-based-method.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next 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.08 seconds. Powered By: Snitz Forums 2000