Delimited String Parsing Functions - Basic Set
Delimited String Parsing Functions - Basic Set
by Jesse Roberge - YeshuaAgapao@gmail.com
Feed it delimited horizontal data and it returns it back as a vertical table.
The basic delimiter function set does not support more than 8000 character delimited strings.
If you need >8000 delimited string support, use the Big delimiter function set. They are much slower, however.
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.
The table variants have some performance gain over the array variants.
If you parsing comma separated proc pameters for a join or for other applications where order doesn't matter, then it is strongly recommended to use the table variant over array variants.
Use the int variants for joining to tables with integer PKs like most identity columns.
Usage:
fn_DelimitToArray ('red,green,yellow,blue,orange,purple',',')
fn_DelimitToIntArray ('1,2,3,4,5,6',',')
fn_DelimitToIntTable ('1,2,3,4,5,6',',')
fn_DelimitToTable ('red,green,yellow,blue,orange,purple',',')
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 - Basic Set
by Jesse Roberge - YeshuaAgapao@gmail.com
Feed it delimited horizontal data and it returns it back as a vertical table.
The basic delimiter function set does not support more than 8000 character delimited strings.
If you need >8000 delimited string support, use the Big delimiter function set. They are much slower, however.
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:
ArrayHas array position index and value data is not casted.
TableNo array position index and value data is not casted.
IntArrayHas array position index and value data is casted to int.
IntTableNo array position index and value data is casted to int.
The table variants have some performance gain over the array variants.
If you parsing comma separated proc pameters for a join or for other applications where order doesn't matter, then it is strongly recommended to use the table variant over array variants.
Use the int variants for joining to tables with integer PKs like most identity columns.
Usage:
fn_DelimitToArray ('red,green,yellow,blue,orange,purple',',')
fn_DelimitToIntArray ('1,2,3,4,5,6',',')
fn_DelimitToIntTable ('1,2,3,4,5,6',',')
fn_DelimitToTable ('red,green,yellow,blue,orange,purple',',')
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') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToArray
GO
CREATE FUNCTION dbo.fn_DelimitToArray
(
@String VarChar(8000),
@Delimiter VarChar(1)
) RETURNS TABLE
AS
RETURN
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID-1), @Delimiter, '')) AS Pos,
SUBSTRING(@String+@Delimiter, PK_CountID, CHARINDEX(@Delimiter, @String+@Delimiter, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter) AND SubString(@Delimiter + @String + @Delimiter, PK_CountID, 1)=@Delimiter
)
GO
--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
IF OBJECT_ID('dbo.fn_DelimitToIntArray') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToIntArray
GO
CREATE FUNCTION dbo.fn_DelimitToIntArray
(
@String VarChar(8000),
@Delimiter VarChar(1)
) RETURNS TABLE
AS
RETURN
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID-1), @Delimiter, '')) AS Pos,
CONVERT(int, SUBSTRING(@String+@Delimiter, PK_CountID, CHARINDEX(@Delimiter, @String+@Delimiter, PK_CountID)-PK_CountID)) AS PK_IntID
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter) AND SubString(@Delimiter + @String + @Delimiter, PK_CountID, 1)=@Delimiter
)
GO
--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
IF OBJECT_ID('dbo.fn_DelimitToIntTable') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToIntTable
GO
CREATE FUNCTION dbo.fn_DelimitToIntTable
(
@String VarChar(8000),
@Delimiter VarChar(1)
) RETURNS TABLE
AS
RETURN
(
SELECT CONVERT(int, SUBSTRING(@String+@Delimiter, PK_CountID, CHARINDEX(@Delimiter, @String+@Delimiter, PK_CountID)-PK_CountID)) AS PK_IntID
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter) AND SubString(@Delimiter + @String + @Delimiter, PK_CountID, 1)=@Delimiter
)
GO
--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
IF OBJECT_ID('dbo.fn_DelimitToTable') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToTable
GO
CREATE FUNCTION dbo.fn_DelimitToTable
(
@String VarChar(8000),
@Delimiter VarChar(10)
) RETURNS TABLE
AS
RETURN
(
SELECT SUBSTRING(@String+@Delimiter, PK_CountID, CHARINDEX(@Delimiter, @String+@Delimiter, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter) AND SubString(@Delimiter + @String + @Delimiter, PK_CountID, 1)=@Delimiter
)
GO
--*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=