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

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

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

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)))
Random Solutions  
 
programming4us programming4us