You can create
user defined function to split Customer IDs like this
CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(8000)
, @sDelimiter VARCHAR(8000) = ','
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList
RETURN
END
GO
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##')
select * from fnSplit('1 22 333 444 5555 666', ' ')
This function you can use in your
Stored Procedure. sample code snippet for using the same function is as follows
select * from Customer_Master where CustomerId IN(SELECT * FROM dbo.fnSplit(@CustomerId, ','))
Where
@CustomerId is parameter of type
varchar. While passing the values of
@CustomerId string can be like C00001,C00002,C00003...
I hope this will help you out. If you need further help please let me know.
If this works fine please accept the solution.
Thanks