Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been using this below piece of code to split the input id like FUNCTION_split('1,2,3',','). Now the problem is it is not working for cases more than 100 in the list. Is there anyway to modify this piece of code to handle more than 100 id's?
CREATE FUNCTION [dbo].[FUNCTION_split]
   (
    @list NVARCHAR(MAX),
    @delimiter NCHAR(1) = ','
     )
  RETURNS TABLE
   AS
   RETURN
     WITH cte_list([BeginChar], [EndChar]) AS (
      SELECT [BeginChar] = CONVERT(BIGINT, 1), [EndChar] = CHARINDEX(@delimiter, @list + @delimiter)
       UNION ALL
        SELECT [BeginChar] = [EndChar] + 1, 
         [EndChar] = CHARINDEX(@delimiter, @list + @delimiter, [EndChar] + 1)
          FROM cte_list WHERE [EndChar] > 0
          )
     SELECT LTRIM(RTRIM(SUBSTRING(@list, [BeginChar],
      CASE WHEN [EndChar] > 0 THEN [EndChar] - [BeginChar] ELSE 0 END))) AS [ParsedValue]
        FROM cte_list WHERE [EndChar] > 0 ;
Posted
Updated 28-Mar-13 7:05am
v2
Comments
Member 9762654 26-Mar-13 2:44am    
what is your front end technology?
Bharath from India 26-Mar-13 3:03am    
I am using this split function inside a stored procedure
Bharath from India 26-Mar-13 3:05am    
select * from dbo.FUNCTION_split('1,2,3',',') OPTION (MAXRECURSION 0)
This in one of the fixes i have figured out..
Any other suggestions are welcome!!
gvprabu 26-Mar-13 3:17am    
Hi, Use Simple logic...

Hi,

Use the following Logic ....
SQL
-- Split Function (Method 1)
CREATE FUNCTION [dbo].[fnSplit](
@delimited NVARCHAR(MAX),
@delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
    SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

    INSERT INTO @t(val)
    SELECT  r.value('.','varchar(MAX)') as item
    FROM  @xml.nodes('/t') as records(r)
RETURN
END
SELECT val FROM fnSplit('Micro Soft,SQL Server 2008, R2',',')
-- Note : If Record Count is Not required then remove ID column from "fnSplit" Function.

-- Split Function (Method 2)
IF OBJECT_ID('fnSplit') IS NOT NULL DROP FUNCTION dbo.fnSplit
GO
CREATE FUNCTION dbo.fnSplit(
    @InputString VARCHAR(8000), -- List of delimited items
    @Delimiter CHAR(1) = ',') -- delimiter that separates items)
RETURNS @List TABLE (ColumnValues VARCHAR(8000))
AS
BEGIN
    DECLARE @NextString NVARCHAR(40), @Pos INT

    SET @InputString = @InputString + @Delimiter
    SET @Pos = CHARINDEX(@Delimiter,@InputString)

    WHILE (@pos <> 0)
    BEGIN
	   SET @NextString = SUBSTRING(@InputString,1,@Pos - 1)
	   INSERT INTO @List(ColumnValues) SELECT @NextString 
	   SET @InputString = SUBSTRING(@InputString,@pos+1,len(@InputString))
	   SET @pos = CHARINDEX(@Delimiter,@InputString)
    END 
    RETURN 
END
GO
SELECT ColumnValues FROM fnSplit('Micro Soft,SQL Server 2008, R2',',')

Regards,
GVPrabu
 
Share this answer
 
v4
try this

create below function
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ParseValues]
(@String varchar(8000), @Delimiter varchar(max) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(max))
AS
BEGIN
    DECLARE @Value varchar(max)
    WHILE @String is not null
    BEGIN
        SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(
          @String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, 
          @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(
          @String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
        INSERT INTO @RESULTS (Val)
        SELECT @Value
    END
RETURN
END


use as below
SQL
select Val from dbo.ParseValues('1,2,3',',')


Happy Coding!
:)
 
Share this answer
 
v2
select * from dbo.FUNCTION_split('1,2,3',',') OPTION (MAXRECURSION 0
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900