Technical Article

Delimited String Parsing Functions - TwoCol set

,

Delimited String Parsing Functions - TwoCol set
by Jesse Roberge - YeshuaAgapao@gmail.com

Feed it two-column delimited horizontal data and it returns it back as a vertical table with the two column data in the same synchronized position order
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 TwoCol delimiter function set, the table variants have negilable performance gain over the array variants because the position indexes is needed internally.

Usage:
fn_DelimitToArray_TwoCol ('red,green,yellow,blue,orange,purple','square,rectangle,circle,triangle,oval,hexagon',',',',') AS ParameterTable JOIN

ON ParameterTable.Value1=
.[any decimal/numeric/character field #1] AND ParameterTable.Value2=
.[any decimal/numeric/character field #2]
fn_DelimitToIntArray_TwoCol ('1,2,3,4,5,6','11,22,33,44,55,66',',',',') AS ParameterTable JOIN
ON ParameterTable.PK_IntID1=
.[int field #1] AND ParameterTable.PK_IntID2=
.[int field #2]
fn_DelimitToIntTable_TwoCol ('1,2,3,4,5,6','11,22,33,44,55,66',',',',') AS ParameterTable JOIN
ON ParameterTable.PK_IntID1=
.[int field #1] AND ParameterTable.PK_IntID2=
.[int field #2]
fn_DelimitToTable_TwoCol ('red,green,yellow,blue,orange,purple','square,rectangle,circle,triangle,oval,hexagon',',',',') AS ParameterTable JOIN
ON ParameterTable.Value1=
.[any decimal/numeric/character field #1] AND ParameterTable.Value2=
.[any decimal/numeric/character field #2]

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 - TwoCol set
by Jesse Roberge - YeshuaAgapao@gmail.com

Feed it two-column delimited horizontal data and it returns it back as a vertical table with the two column data in the same synchronized position order
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.
In the TwoCol delimiter function set, the table variants have negilable performance gain over the array variants because the position indexes is needed internally.

Usage:
fn_DelimitToArray_TwoCol ('red,green,yellow,blue,orange,purple','square,rectangle,circle,triangle,oval,hexagon',',',',') AS ParameterTable JOIN [table] ON ParameterTable.Value1=[table].[any decimal/numeric/character field #1] AND ParameterTable.Value2=[table].[any decimal/numeric/character field #2]
fn_DelimitToIntArray_TwoCol ('1,2,3,4,5,6','11,22,33,44,55,66',',',',') AS ParameterTable JOIN [table] ON ParameterTable.PK_IntID1=[table].[int field #1] AND ParameterTable.PK_IntID2=[table].[int field #2]
fn_DelimitToIntTable_TwoCol ('1,2,3,4,5,6','11,22,33,44,55,66',',',',') AS ParameterTable JOIN [table] ON ParameterTable.PK_IntID1=[table].[int field #1] AND ParameterTable.PK_IntID2=[table].[int field #2]
fn_DelimitToTable_TwoCol ('red,green,yellow,blue,orange,purple','square,rectangle,circle,triangle,oval,hexagon',',',',') AS ParameterTable JOIN [table] ON ParameterTable.Value1=[table].[any decimal/numeric/character field #1] AND ParameterTable.Value2=[table].[any decimal/numeric/character field #2]

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_TwoCol') IS NOT NULL DROP FUNCTION dbo.fn_DelimitToArray_TwoCol
GO

CREATE FUNCTION dbo.fn_DelimitToArray_TwoCol
(
@String VarChar(8000),
@String2 VarChar(8000),
@Delimiter VarChar(1),
@Delimiter2 VarChar(1)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1.Pos, Counter1.Value AS Value1, Counter2.Value AS Value2
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID), @Delimiter, '')) + 1 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
) AS Counter1
FULL OUTER JOIN (
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String2, PK_CountID), @Delimiter2, '')) + 1 AS Pos,
SUBSTRING(@String2+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, @String2+@Delimiter2, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String2)+LEN(@Delimiter2) AND SubString(@Delimiter2 + @String2 + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2 ON Counter1.Pos=Counter2.Pos
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToIntArray_TwoCol
(
@String VarChar(8000),
@String2 VarChar(8000),
@Delimiter VarChar(1),
@Delimiter2 VarChar(1)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1.Pos, Counter1.PK_IntID AS PK_IntID1, Counter2.PK_IntID AS PK_IntID2
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID), @Delimiter, '')) + 1 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
) AS Counter1
FULL OUTER JOIN (
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String2, PK_CountID), @Delimiter2, '')) + 1 AS Pos,
CONVERT(int, SUBSTRING(@String2+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, @String2+@Delimiter2, PK_CountID)-PK_CountID)) AS PK_IntID
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String2)+LEN(@Delimiter2) AND SubString(@Delimiter2 + @String2 + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2 ON Counter1.Pos=Counter2.Pos
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToIntTable_TwoCol
(
@String VarChar(8000),
@String2 VarChar(8000),
@Delimiter VarChar(1),
@Delimiter2 VarChar(1)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1.PK_IntID AS PK_IntID1, Counter2.PK_IntID AS PK_IntID2
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID), @Delimiter, '')) + 1 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
) AS Counter1
FULL OUTER JOIN (
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String2, PK_CountID), @Delimiter2, '')) + 1 AS Pos,
CONVERT(int, SUBSTRING(@String2+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, @String2+@Delimiter2, PK_CountID)-PK_CountID)) AS PK_IntID
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String2)+LEN(@Delimiter2) AND SubString(@Delimiter2 + @String2 + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2 ON Counter1.pos=Counter2.pos
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToTable_TwoCol
(
@String VarChar(8000),
@String2 VarChar(8000),
@Delimiter VarChar(1),
@Delimiter2 VarChar(1)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1.Value AS Value1, Counter2.Value AS Value2
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID), @Delimiter, '')) + 1 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
) AS Counter1
FULL OUTER JOIN (
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String2, PK_CountID), @Delimiter2, '')) + 1 AS Pos,
SUBSTRING(@String2+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, @String2+@Delimiter2, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String2)+LEN(@Delimiter2) AND SubString(@Delimiter2 + @String2 + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2 ON Counter1.Pos=Counter2.Pos
)
GO

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

Rate

1.67 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

1.67 (3)

You rated this post out of 5. Change rating