Solved

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

Posted on 2012-03-23
6
477 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now