Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-23
6
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 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 51

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 46

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 46

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

618 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