Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favourite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved in your SSC briefcase from the favorites tab.
Download now (direct download link)

Delimited String Parser

By Jano Petras,

Delimited String Parser

Many times there is a need from the business logic layer to pass a list of items into stored procedure . As T-SQL has no concept of an array, the delimited strings are way to go. Most likely the required argument will be an "array" of integers (IDs), but it can be other data, or multidimensional array.

This function parses up to 9-dimensions delimited string data and generates a temporary table with the inidividual records.

The returned table always had 9 columns (named Field1, Field2, ... FIeld9), even if only 1 is used (the rest is populated with NULLs, therefore should not add much of the memory overhead).

Some example usages for example:

DECLARE @delimitedData VARCHAR(MAX)
SET @delimitedData =
'1023|Count of attempts|20
1045|Possible hacking|12
2010|Abandoned sessions|3231
2021|Hacked URLs|211'
CREATE TABLE #Array (ID INT PRIMARY KEY, [Description] VARCHAR(255), [Value] INT)
INSERT INTO #Array (ID, [Description], [Value])
SELECT Field1, Field2, Field3 FROM dbo.ufn_ParseDelimitedString(@delimitedData, '|', char(13)+char(10))
SELECT * FROM #Array
DROP TABLE #Array

OUTPUT from this is:
ID Description Value
==============================================
1023 Count of attempts 20
1045 Possible hacking 12
2010 Abandoned sessions 3231
2021 Hacked URLs 211


The function also handles missing (NULL) values:
DECLARE @delimitedData VARCHAR(MAX)
SET @delimitedData =
'1023|Count of attempts|
1045||12
2010|Abandoned sessions|3231
2021|Hacked URLs|211'

CREATE TABLE #Array (ID INT PRIMARY KEY, [Description] VARCHAR(255), [Value] INT)
INSERT INTO #Array (ID, [Description], [Value])
SELECT Field1, Field2, Field3 FROM dbo.ufn_ParseDelimitedString(@delimitedData, '|', char(13)+char(10))
SELECT * FROM #Array
DROP TABLE #Array

OUTPUT IS:
ID Description Value
==============================================
1023 Count of attempts NULL
1045 NULL 12
2010 Abandoned sessions 3231
2021 Hacked URLs 211

Total article views: 2441 | Views in the last 30 days: 17
 
Related Articles
FORUM

Arrays in SQL SERVER

Arrays

FORUM

Advice for array oflarge arrays

how would you represent an array of 1020 floats in record

FORUM

Arrays

Array query?

FORUM

Array in SQL server

Array in SQL server

FORUM

abandon Redgate for Compellent?

Our network folks are using Compellent Storage Center for their file backups and DR. They are pushi...

Tags
delimited data    
delimited strings    
parse    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones