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 2008 Forums
 Transact-SQL (2008)
 @parameter question

Author  Topic 

hbadministrator
Posting Yak Master

120 Posts

Posted - 2013-05-06 : 12:36:35
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
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-06 : 12:45:33
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 - 2013-05-06 : 14:50:26
Thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-07 : 00:37:05
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
   

- Advertisement -