Solved

splitting a delimited list into an "array" with T-SQL

Posted on 2012-03-23
6
485 Views
Last Modified: 2012-03-26
I can find umpteen articles on the web about getting around the "array problem" in T-SQL.  But I'm looking for a solution that's the easiest.

Picture a SQL table containing delimited text strings laid out basically like so:

Option 1=10;Something Else=20;Other=99

I want to parse this, first by semicolon delimiters, then by the = sign into key-value pairs.

What's the easiest way to do this that YOU use?

Thanks for the help.
0
Comment
Question by:bamapie
6 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 37760104
* Do the parsing in two passes

* This is an excellent article on parsing, in case you haven't already read it
http:/A_1536-delimited-list-as-parameter-what-are-the-options.html

* Jeff Moden has a wonderful article and long discussion on the fastest parsing method over at SQL Server Central (do a search for "Tally OH"

* how often do you need to do this parsing?

* how critical is performance?
0
 
LVL 37

Expert Comment

by:ValentinoV
ID: 37760422
Another option which aikimark didn't mention yet, and I might see myself using (depending on the situation) is Integration Services.

Given a string like that and seeing that you'd like to split it up in Key/Value pairs, this seems to be part of a data load process somehow.  So I'd probably create an SSIS package to handle that.  In the package I would use a Script Component in the Data Flow to do the parsing in C#.  VB.NET would of course also be an option.  .NET has got excellent string manipulation functions.

But again, it all depends on your situation.  So there you are, one more option :)

If you need further info on this method, just ask!
Valentino.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 37760940
The *easiest* method I know of is to normalize the data.  No parsing necessary when you can use SELECT..FROM to get a cursor of name/value rows.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 45

Expert Comment

by:aikimark
ID: 37762936
Microsoft offers this tutorial article on parsing using the power of the .Net language.  In the Tally OH article thread, the conclusion was that, although T-SQL could perform parsing very fast, its performance couldn't touch that of .Net (plug-in) parsing.
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37762974
Since your data pattern is simple, you might also approach the solution through an transformation from string to XML.  Once in XML format, your T-SQL can use XPath to get the name/value pairs' data.

Example, using your data sample:
'<NVdata><ItemName>' + Replace(Replace(myfield, ';', '</ItemValue><ItemName>'), '=', '</ItemName><ItemValue>') + '</ItemValue></NVdata>'


Result (formatted for readability.
<NVdata>
<ItemName>Option 1</ItemName><ItemValue>10</ItemValue>
<ItemName>Something Else</ItemName><ItemValue>20</ItemValue>
<ItemName>Other</ItemName><ItemValue>99</ItemValue>
</NVdata>

Open in new window


Replace reference:
http://msdn.microsoft.com/en-us/library/ms186862.aspx

===================
If that seems a bit verbose, you can shorten the XML tags and achieve something like this:
'<NV><N>' + Replace(Replace(myfield, ';', '</V><N>'), '=', '</N><V>') + '</V></NV>'

Formatted view:
<NV>
<N>Option 1</N><V>10</V>
<N>Something Else</N><V>20</V>
<N>Other</N><V>99</V>
</NV>

Open in new window

0
 

Author Closing Comment

by:bamapie
ID: 37766153
Thanks!  

By the way, this kind of parsing is a rare activity.  The results I do write into a table from which they're actually used.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question