Technical Article

Delimited String Parsing Functions - 2D set

,

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

Feed it double-delimited horizontal data and it returns it back as a non-pivoted vertical table with a 2-diemensional star schema.
The 2D function set does not support more than 8000 character delimited strings.
If you need >8000 delimited string support, use the Big2D 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.
SQL Server 2005 only.

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:
fn_DelimitToArray_2D ('red^square^square1^square2,green^rectangle^rectangle1^rectangle2,yellow^circle^circle1^circle2,blue^triangle^triangle1^triangle2,orange^oval^oval1^oval2,purple^hexagon^hexagon1^hexagon2',',','^')
fn_DelimitToIntArray_2D ('1^11^111,2^22^222,3^33^333,4^44^444,5^55^555',',','^')
fn_DelimitToIntTable_2D ('1^11^111,2^22^222,3^33^333,4^44^444,5^55^555',',','^')
fn_DelimitToTable_2D ('red^square^square1^square2,green^rectangle^rectangle1^rectangle2,yellow^circle^circle1^circle2,blue^triangle^triangle1^triangle2,orange^oval^oval1^oval2,purple^hexagon^hexagon1^hexagon2',',','^')

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

Feed it double-delimited horizontal data and it returns it back as a non-pivoted vertical table with a 2-diemensional star schema.
The 2D function set does not support more than 8000 character delimited strings.
If you need >8000 delimited string support, use the Big2D 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.
SQL Server 2005 only.

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 Big2D delimiter function set, the table variants have some performance gain over the array variants, but are not very useful except in joins.

Usage:
fn_DelimitToArray_2D ('red^square^square1^square2,green^rectangle^rectangle1^rectangle2,yellow^circle^circle1^circle2,blue^triangle^triangle1^triangle2,orange^oval^oval1^oval2,purple^hexagon^hexagon1^hexagon2',',','^')
fn_DelimitToIntArray_2D ('1^11^111,2^22^222,3^33^333,4^44^444,5^55^555',',','^')
fn_DelimitToIntTable_2D ('1^11^111,2^22^222,3^33^333,4^44^444,5^55^555',',','^')
fn_DelimitToTable_2D ('red^square^square1^square2,green^rectangle^rectangle1^rectangle2,yellow^circle^circle1^circle2,blue^triangle^triangle1^triangle2,orange^oval^oval1^oval2,purple^hexagon^hexagon1^hexagon2',',','^')

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

CREATE FUNCTION dbo.fn_DelimitToArray_2D
(
@String VarChar(8000),
@Delimiter1 VarChar(10),
@Delimiter2 VarChar(10)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1st.Pos AS RowPos, Counter2nd.Pos AS ColPos, Counter2nd.Value AS Value
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID-1), @Delimiter1, '')) AS Pos,
SUBSTRING(@String+@Delimiter1, PK_CountID, CHARINDEX(@Delimiter1, @String+@Delimiter1, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter1) AND SubString(@Delimiter1 + @String + @Delimiter1, PK_CountID, 1)=@Delimiter1
) AS Counter1st
CROSS APPLY (
SELECT
PK_CountID - LEN(REPLACE(LEFT(Counter1st.Value, PK_CountID-1), @Delimiter2, '')) AS Pos,
SUBSTRING(Counter1st.Value+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, Counter1st.Value+@Delimiter2, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(Counter1st.Value)+LEN(@Delimiter2) AND SubString(@Delimiter2 + Counter1st.Value + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2nd
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToIntArray_2D
(
@String VarChar(8000),
@Delimiter1 VarChar(10),
@Delimiter2 VarChar(10)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter1st.Pos AS RowPos, Counter2nd.Pos AS ColPos, CONVERT(int, Counter2nd.value) AS PK_IntID
FROM
(
SELECT
PK_CountID - LEN(REPLACE(LEFT(@String, PK_CountID-1), @Delimiter1, '')) AS Pos,
SUBSTRING(@String+@Delimiter1, PK_CountID, CHARINDEX(@Delimiter1, @String+@Delimiter1, PK_CountID)-PK_CountID) AS value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter1) AND SubString(@Delimiter1 + @String + @Delimiter1, PK_CountID, 1)=@Delimiter1
) AS Counter1st
CROSS APPLY (
SELECT
PK_CountID - LEN(REPLACE(LEFT(Counter1st.value, PK_CountID-1), @Delimiter2, '')) AS Pos,
SUBSTRING(Counter1st.value+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, Counter1st.value+@Delimiter2, PK_CountID)-PK_CountID) AS value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(Counter1st.value)+LEN(@Delimiter2) AND SubString(@Delimiter2 + Counter1st.value + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2nd
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToIntTable_2D
(
@String VarChar(8000),
@Delimiter1 VarChar(10),
@Delimiter2 VarChar(10)
) RETURNS TABLE
AS

RETURN
(
SELECT CONVERT(int, Counter2nd.Value) AS PK_IntID
FROM
(
SELECT
SUBSTRING(@String+@Delimiter1, PK_CountID, CHARINDEX(@Delimiter1, @String+@Delimiter1, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter1) AND SubString(@Delimiter1 + @String + @Delimiter1, PK_CountID, 1)=@Delimiter1
) AS Counter1st
CROSS APPLY (
SELECT
SUBSTRING(Counter1st.Value+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, Counter1st.Value+@Delimiter2, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(Counter1st.Value)+LEN(@Delimiter2) AND SubString(@Delimiter2 + Counter1st.Value + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2nd
)
GO

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

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

CREATE FUNCTION dbo.fn_DelimitToTable_2D
(
@String VarChar(8000),
@Delimiter1 VarChar(10),
@Delimiter2 VarChar(10)
) RETURNS TABLE
AS

RETURN
(
SELECT Counter2nd.Value AS Value
FROM
(
SELECT
SUBSTRING(@String+@Delimiter1, PK_CountID, CHARINDEX(@Delimiter1, @String+@Delimiter1, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(@String)+LEN(@Delimiter1) AND SubString(@Delimiter1 + @String + @Delimiter1, PK_CountID, 1)=@Delimiter1
) AS Counter1st
CROSS APPLY (
SELECT
SUBSTRING(Counter1st.Value+@Delimiter2, PK_CountID, CHARINDEX(@Delimiter2, Counter1st.Value+@Delimiter2, PK_CountID)-PK_CountID) AS Value
FROM dbo.counter WITH (NOLOCK)
WHERE PK_CountID >0 AND PK_CountID<LEN(Counter1st.Value)+LEN(@Delimiter2) AND SubString(@Delimiter2 + Counter1st.Value + @Delimiter2, PK_CountID, 1)=@Delimiter2
) AS Counter2nd
)
GO

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

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating