Solved

split string so it can be used for a where in statement

Posted on 2010-08-24
7
343 Views
Last Modified: 2012-05-10
I'd like to search for a number within a string, however the field in the table is a number
eg

MYSTRING IS PASSED AS A STRING TO THE STORED PROCEDURE
   select*
   from tbWholesalerProduct
     inner join tbProduct on (tbProduct.iProductId = tbWholesalerProduct.iProductId)
     inner join tbProductGroup on (tbProductGroup.iProductGroupId = tbProduct.iProductGroupId)
     inner join dbo.tbWholesalerProductPrice ON dbo.tbWholesalerProduct.iWholesalerProductId = dbo.tbWholesalerProductPrice.iWholesalerproductid
     where    (dbo.tbWholesalerProductPrice.vState like @vState)
      and    (tbWholesalerProduct.iWholesalerId = @iWholesalerId)
         and (tbProduct.iProductGroupId = @iProductGroupId) and (tbProduct.cStatus = 'A')
and  (tbProduct.iProductid in MYSTRING) -- iProductid  is an int column

Thanks
0
Comment
Question by:lgreally
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Expert Comment

by:rogerperkins
ID: 33517215
Try this:

  select*
   from tbWholesalerProduct
     inner join tbProduct on (tbProduct.iProductId = tbWholesalerProduct.iProductId)
     inner join tbProductGroup on (tbProductGroup.iProductGroupId = tbProduct.iProductGroupId)
     inner join dbo.tbWholesalerProductPrice ON dbo.tbWholesalerProduct.iWholesalerProductId = dbo.tbWholesalerProductPrice.iWholesalerproductid
     where    (dbo.tbWholesalerProductPrice.vState like @vState)
      and    (tbWholesalerProduct.iWholesalerId = @iWholesalerId)
         and (tbProduct.iProductGroupId = @iProductGroupId) and (tbProduct.cStatus = 'A')
and  (tbProduct.iProductid = cast(MYSTRING as int))
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33517241
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33517244
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:lgreally
ID: 33517255
Hi Roger,

MYSTRING will be equal to something like
(1,12,4)

so it would need to work as an IN statement
where (tbProduct.iProductid in MYSTRING)
0
 
LVL 2

Expert Comment

by:rogerperkins
ID: 33517415
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 75 total points
ID: 33517591
You can greate a function to convert the list into a table of integers then use the function in your query
Something like

CREATE FUNCTION [dbo].[GET_INT_TABLE]
(
      @STRINPUT VARCHAR(MAX)      
)
RETURNS @INTTABLE TABLE
(
      VALUE INT
)
AS
BEGIN
      DECLARE @insertvalue varchar(32)
      DECLARE @pos INT
      DECLARE @nextpos INT
      DECLARE @valuelen INT

      SELECT @pos = 0, @nextpos = 1
      WHILE @nextpos > 0
      BEGIN
            SELECT @nextpos = charindex(',', @STRINPUT, @pos + 1)
            SELECT @valuelen = CASE
                              WHEN @nextpos > 0 THEN
                                    @nextpos
                                          ELSE
                                    len(@STRINPUT) + 1
                                   END - @pos - 1
         
        set @insertvalue = (substring(@STRINPUT, @pos + 1, @valuelen))                      
        if IsNumeric(@insertvalue) = 1
        begin
                  INSERT @INTTABLE (VALUE)
                  VALUES (@insertvalue)
            end
            SELECT @pos = @nextpos
      END
            
      RETURN       
END

Then in your stored procedure query
....
where (tbProduct.iProductid in (select value from GET_INT_TABLE(@MYSTRING)))
0
 

Author Closing Comment

by:lgreally
ID: 33518111
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now