Connecting Tech Pros Worldwide Help | Site Map

Can I use LIKE with IN?

dcharnigo's Avatar
Newbie
 
Join Date: Feb 2007
Location: Ohio
Posts: 20
#1: Aug 5 '09
I found this example of passing a list into a stored procedure:

Expand|Select|Wrap|Line Numbers
  1. CREATE PROC dbo.GetOrderList1
  2. (
  3.     @OrderList varchar(500)
  4. )
  5. AS
  6. BEGIN
  7.     SET NOCOUNT ON
  8.  
  9.     DECLARE @SQL varchar(600)
  10.  
  11.     SET @SQL = 
  12.     'SELECT OrderID, CustomerID, EmployeeID, OrderDate
  13.     FROM dbo.Orders
  14.     WHERE OrderID IN (' + @OrderList + ')'
  15.  
  16.     EXEC(@SQL)    
  17. END
  18. GO
  19.  
where OrderList is something like "100,200,300,400".

What I want to do is find OrderID's that begin with any of the items in the list, so WHERE OrderID LIKE '100%' OR LIKE '200%' etc [WHERE OrderID LIKE IN...]. Is something like this possible?

Thanks.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Aug 7 '09

re: Can I use LIKE with IN?


Read this

Happy Coding!!!


--- CK
dcharnigo's Avatar
Newbie
 
Join Date: Feb 2007
Location: Ohio
Posts: 20
#3: Aug 7 '09

re: Can I use LIKE with IN?


CK--

I read over the article, it seems like it was more about using REGEX and WILDCARDS. What I was trying to avoid was splitting the string apart and building a dynamic query. Like I said before I am going to pass in an array of about 6 numbers that the column might start with. The value is 28 digits long, I am going to pass in the first 4 or 5 digits, I want to pull all the records that match the set of the first 4 or 5 digits. Example:

Using the code below data will be (I want the columns <--):

@OrderList = (12345,54321)

CIF--------------------------------- OTHER COLUMN(S)
------------------------------------------------------------------------
1234500000000000045345 <--RETURN THIS
1234500008631362351351 <--RETURN THIS
9876834289374237423942
6566743234213123131312
5432100000000000023131 <--RETURN THIS
1234500000007637621369 <--RETURN THIS
5432108879796779697999 <--RETURN THIS
9410273734645535355355

Was there something I missed in the article?

Thanks,

Dan
Reply