Link to home
Start Free TrialLog in
Avatar of bamapie
bamapie

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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
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

Avatar of bamapie
bamapie

ASKER

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.