Solved

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

Posted on 2012-03-23
6
493 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 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 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.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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