Technical Article

Delimited String Parsing Functions - Big Set

,

Delimited String Parsing Functions - Big Set
by Jesse Roberge - YeshuaAgapao@gmail.com
Update: Added robustness for NULL inputs and made it return no rows on blank inputs.

Feed it large strings of delimited horizontal data and it returns it back as a vertical table.
The Big function set supports more than 8000 character delimited strings, but the individual elements must be 8000 characters or less.
If you like performance you don't need to process delimited strings over 8000 characters, then use the basic delimiter function set instead of the Big delimiter function set.
Requires a table of numbers. These functions expect it to be called 'Counter' in the same database that you save these functions to.
Search for 'Counter table (table of numbers) setter-upper for SQL Server 2005' or Counter table (table of numbers) setter-upper for SQL Server 2000' if you need a script to set this up for you.
Works in both SQL Server 2000 and 2005.

Variants:
Array Has array position index and value data is not casted.
Table No array position index and value data is not casted.
IntArray Has array position index and value data is casted to int.
IntTable No array position index and value data is casted to int.
In the Big2D delimiter function set, the table variants have some performance gain over the array variants, but are not very useful except in joins.

Usage:
SELECT * FROM dbo.fn_DelimitToArray_Big ('red,green,yellow,blue,orange,purple',',') AS Delimit
SELECT * FROM dbo.fn_DelimitToIntArray_Big('1111,22,333,444,5555,66',',') AS Delimit
SELECT * FROM dbo.fn_DelimitToIntTable_Big ('1111,22,333,444,5555,66',',') AS Delimit
SELECT * FROM dbo.fn_DelimitToTable_Big ('red,green,yellow,blue,orange,purple',',') AS Delimit

Copyright:
Licensed under the L-GPL - a weak copyleft license - you are permitted to use this as a component of a proprietary database and call this from proprietary software.
Copyleft lets you do anything you want except plagarize, conceal the source, or prohibit copying & re-distribution of this script/proc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

see <http://www.fsf.org/licensing/licenses/lgpl.html> for the license text.

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

/*
Delimited String Parsing Functions - Big Set
by Jesse Roberge - YeshuaAgapao@gmail.com
Update: Added robustness for NULL inputs and made it return no rows on blank inputs.

Feed it large strings of delimited horizontal data and it returns it back as a vertical table.
The Big function set supports more than 8000 character delimited strings, but the individual elements must be 8000 characters or less.
If you like performance you don't need to process delimited strings over 8000 characters, then use the basic delimiter function set instead of the Big delimiter function set.
Requires a table of numbers.  These functions expect it to be called 'Counter' in the same database that you save these functions to.
Search for 'Counter table (table of numbers) setter-upper for SQL Server 2005' or Counter table (table of numbers) setter-upper for SQL Server 2000' if you need a script to set this up for you.
Works in both SQL Server 2000 and 2005.

Variants:
	Array		Has array position index and value data is not casted.
	Table		No array position index and value data is not casted.
	IntArray	Has array position index and value data is casted to int.
	IntTable	No array position index and value data is casted to int.
In the Big2D delimiter function set, the table variants have some performance gain over the array variants, but are not very useful except in joins.

Usage:
	SELECT * FROM dbo.fn_DelimitToArray_Big ('red,green,yellow,blue,orange,purple',',') AS Delimit
	SELECT * FROM dbo.fn_DelimitToIntArray_Big('1111,22,333,444,5555,66',',') AS Delimit
	SELECT * FROM dbo.fn_DelimitToIntTable_Big ('1111,22,333,444,5555,66',',') AS Delimit
	SELECT * FROM dbo.fn_DelimitToTable_Big ('red,green,yellow,blue,orange,purple',',') AS Delimit

Copyright:
	Licensed under the L-GPL - a weak copyleft license - you are permitted to use this as a component of a proprietary database and call this from proprietary software.
	Copyleft lets you do anything you want except plagarize, conceal the source, or prohibit copying & re-distribution of this script/proc.

	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    see <http://www.fsf.org/licensing/licenses/lgpl.html> for the license text.
*/

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

IF OBJECT_ID('dbo.fn_DelimitToArray_Big') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToArray_Big
GO

CREATE FUNCTION dbo.fn_DelimitToArray_Big
	(
		@String text,
		@Delimiter VarChar(1)
	)
RETURNS @T TABLE
	(
		Pos int NOT NULL,
		Value VarChar(8000) NOT NULL
	)
AS

BEGIN

	DECLARE @Slices Table
	(
		Slice VarChar(8000) NOT NULL,
		CumulativeElementCount int NOT NULL
	)

	DECLARE @Slice VarChar(8000)
	DECLARE @TextPos int
	DECLARE @MaxLength int
	DECLARE @StopPos int
	DECLARE @StringLength int
	DECLARE @CumulativeElementCount int
	SELECT @TextPos = 1, @MaxLength = 8000 - 2, @CumulativeElementCount=0
	SELECT @StringLength=ISNULL(DATALENGTH(@String),0)-@MaxLength

	WHILE @TextPos < @StringLength
	BEGIN
		SELECT @Slice = SUBSTRING(@String, @TextPos, @MaxLength)
		SELECT @StopPos = @MaxLength - CHARINDEX(@Delimiter, REVERSE(@Slice))

		INSERT INTO @Slices (Slice, CumulativeElementCount) VALUES (@Delimiter + LEFT(@Slice, @StopPos) + @Delimiter, @CumulativeElementCount)

		SELECT @CumulativeElementCount=@CumulativeElementCount+LEN(@Slice)-LEN(REPLACE(@Slice, @Delimiter, ''))
		SELECT @TextPos = @TextPos + @StopPos + 1
	END
	IF @StringLength>0-@MaxLength INSERT INTO @Slices (Slice, CumulativeElementCount) VALUES (@Delimiter + SUBSTRING(@String, @TextPos, @MaxLength) + @Delimiter, @CumulativeElementCount);

	INSERT INTO @T (Pos, Value)
	SELECT Pos, Value
	FROM
		(
			SELECT
				PK_CountID - LEN(REPLACE(LEFT(Slices.Slice, PK_CountID-1), @Delimiter, '')) + Slices.CumulativeElementCount AS Pos,
				SUBSTRING(Slices.Slice, Counter.PK_CountID + 1, CHARINDEX(@Delimiter, Slices.Slice, Counter.PK_CountID + 1) - Counter.PK_CountID - 1) AS Value
			FROM
				dbo.Counter WITH (NOLOCK)
				JOIN @Slices AS Slices ON
					Counter.PK_CountID>0 AND Counter.PK_CountID <= LEN(Slices.Slice) - 1 AND
					SUBSTRING(Slices.Slice, Counter.PK_CountID, 1) = @Delimiter
		) AS StringGet
	RETURN
END
GO

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

IF OBJECT_ID('dbo.fn_DelimitToIntArray_Big') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToIntArray_Big
GO

CREATE FUNCTION dbo.fn_DelimitToIntArray_Big
	(
		@String text,
		@Delimiter VarChar(1)
	)
RETURNS @T TABLE
	(
		Pos int NOT NULL,
		PK_IntID int NOT NULL
	)
AS

BEGIN

	DECLARE @Slices Table
	(
		Slice VarChar(8000) NOT NULL,
		CumulativeElementCount int NOT NULL
	)

	DECLARE @Slice VarChar(8000)
	DECLARE @TextPos int
	DECLARE @MaxLength int
	DECLARE @StopPos int
	DECLARE @StringLength int
	DECLARE @CumulativeElementCount int
	SELECT @TextPos = 1, @MaxLength = 8000 - 2, @CumulativeElementCount=0
	SELECT @StringLength=ISNULL(DATALENGTH(@String),0)-@MaxLength

	WHILE @TextPos < @StringLength
	BEGIN
		SELECT @Slice = SUBSTRING(@String, @TextPos, @MaxLength)
		SELECT @StopPos = @MaxLength - CHARINDEX(@Delimiter, REVERSE(@Slice))

		INSERT INTO @Slices (Slice, CumulativeElementCount) VALUES (@Delimiter + LEFT(@Slice, @StopPos) + @Delimiter, @CumulativeElementCount)

		SELECT @CumulativeElementCount=@CumulativeElementCount+LEN(@Slice)-LEN(REPLACE(@Slice, @Delimiter, ''))
		SELECT @TextPos = @TextPos + @StopPos + 1
	END
	IF @StringLength>0-@MaxLength INSERT INTO @Slices (Slice, CumulativeElementCount) VALUES (@Delimiter + SUBSTRING(@String, @TextPos, @MaxLength) + @Delimiter, @CumulativeElementCount);

	INSERT INTO @T (Pos, PK_IntID)
	SELECT Pos, PK_IntID
	FROM
		(
			SELECT
				PK_CountID - LEN(REPLACE(LEFT(Slices.Slice, PK_CountID-1), @Delimiter, '')) + Slices.CumulativeElementCount AS Pos,
				CONVERT(int, SUBSTRING(Slices.Slice, Counter.PK_CountID + 1, CHARINDEX(@Delimiter, Slices.Slice, Counter.PK_CountID + 1) - Counter.PK_CountID - 1)) AS PK_IntID
			FROM
				dbo.Counter WITH (NOLOCK)
				JOIN @Slices AS Slices ON
					Counter.PK_CountID>0 AND Counter.PK_CountID <= LEN(Slices.Slice) - 1 AND
					SUBSTRING(Slices.Slice, Counter.PK_CountID, 1) = @Delimiter
		) AS StringGet
	RETURN
END
GO

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

IF OBJECT_ID('dbo.fn_DelimitToIntTable_Big') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToIntTable_Big
GO

CREATE FUNCTION dbo.fn_DelimitToIntTable_Big
	(
		@String text,
		@Delimiter VarChar(1)
	)
RETURNS @T TABLE
	(
		PK_IntID int NOT NULL
	)
AS

BEGIN

	DECLARE @Slices Table
	(
		Slice VarChar(8000) NOT NULL
	)

	DECLARE @Slice VarChar(8000)
	DECLARE @TextPos int
	DECLARE @MaxLength int
	DECLARE @StopPos int
	DECLARE @StringLength int
	SELECT @TextPos = 1, @MaxLength = 8000 - 2
	SELECT @StringLength=ISNULL(DATALENGTH(@String),0)-@MaxLength

	WHILE @TextPos < @StringLength
	BEGIN
		SELECT @Slice = SUBSTRING(@String, @TextPos, @MaxLength)
		SELECT @StopPos = @MaxLength - CHARINDEX(@Delimiter, REVERSE(@Slice))

		INSERT INTO @Slices (Slice) VALUES (@Delimiter + LEFT(@Slice, @StopPos) + @Delimiter)

		SELECT @TextPos = @TextPos + @StopPos + 1
	END
	IF @StringLength>0-@MaxLength INSERT INTO @Slices (slice) VALUES (@Delimiter + SUBSTRING(@String, @TextPos, @MaxLength) + @Delimiter);

	INSERT INTO @T (PK_IntID)
	SELECT PK_IntID
	FROM
		(
			SELECT
				CONVERT(int, SUBSTRING(Slices.Slice, Counter.PK_CountID + 1, CHARINDEX(@Delimiter, Slices.Slice, Counter.PK_CountID + 1) - Counter.PK_CountID - 1)) AS PK_IntID
			FROM
				dbo.Counter WITH (NOLOCK)
				JOIN @Slices AS Slices ON
					Counter.PK_CountID>0 AND Counter.PK_CountID <= LEN(Slices.Slice) - 1 AND
					SUBSTRING(Slices.Slice, Counter.PK_CountID, 1) = @Delimiter
		) AS StringGet
	RETURN
END
GO

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

IF OBJECT_ID('dbo.fn_DelimitToTable_Big') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToTable_Big
GO

CREATE FUNCTION dbo.fn_DelimitToTable_Big
	(
		@String text,
		@Delimiter VarChar(1)
	)
RETURNS @T TABLE
	(
		Value VarChar(8000) NOT NULL
	)
AS

BEGIN

	DECLARE @Slices Table
	(
		Slice VarChar(8000) NOT NULL
	)

	DECLARE @Slice VarChar(8000)
	DECLARE @TextPos int
	DECLARE @MaxLength int
	DECLARE @StopPos int
	DECLARE @StringLength int
	SELECT @TextPos = 1, @MaxLength = 8000 - 2
	SELECT @StringLength=ISNULL(DATALENGTH(@String),0)-@MaxLength

	WHILE @TextPos < @StringLength
	BEGIN
		SELECT @Slice = SUBSTRING(@String, @TextPos, @MaxLength)
		SELECT @StopPos = @MaxLength - CHARINDEX(@Delimiter, REVERSE(@Slice))

		INSERT INTO @Slices (Slice) VALUES (@Delimiter + LEFT(@Slice, @StopPos) + @Delimiter)

		SELECT @TextPos = @TextPos + @StopPos + 1
	END
	IF @StringLength>0-@MaxLength INSERT INTO @Slices (slice) VALUES (@Delimiter + SUBSTRING(@String, @TextPos, @MaxLength) + @Delimiter);

	INSERT INTO @T (Value)
	SELECT Value
	FROM
		(
			SELECT
				SUBSTRING(Slices.Slice, Counter.PK_CountID + 1, CHARINDEX(@Delimiter, Slices.Slice, Counter.PK_CountID + 1) - Counter.PK_CountID - 1) AS Value
			FROM
				dbo.Counter WITH (NOLOCK)
				JOIN @Slices AS Slices ON
					Counter.PK_CountID>0 AND Counter.PK_CountID <= LEN(Slices.Slice) - 1 AND
					SUBSTRING(Slices.Slice, Counter.PK_CountID, 1) = @Delimiter
		) AS StringGet
	RETURN
END
GO

--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

Rate

3 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (2)

You rated this post out of 5. Change rating