Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Getting Data Out of Delimited List

 
Goto page 1, 2, 3
   Database Help (Home) -> Programming RSS
Next:  Wildcard NVARCHAR search  
Author Message
geoffrobinson

External


Since: May 13, 2008
Posts: 2



(Msg. 1) Posted: Tue May 13, 2008 12:46 pm
Post subject: Getting Data Out of Delimited List
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I've done a bunch of searches about the most efficient way to pull
data out of delimited list, but most if not all of the results involve
creating a delimited list. So I wanted to run this question by the
group.

If I have a delimited list along these lines:
manhattan.nyc.ny.usa.world

....what would the best (or a good) way of getting just the second and
third elements, for example, be?

At first I was thinking a loop with a cursor would be in order. But
then my thoughts were turning to using Perl on the flat-files these
were derived from. Also, using .NET in a stored procedure. But I would
rather have a SQL solution to the problem.

I appreciate any help in advance.

thanks,
Geoff

 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 2) Posted: Tue May 13, 2008 1:58 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 13, 3:09 pm, "Plamen Ratchev" wrote:
> Using CLR will give you very good performance on this type of split.
>
> Erland Sommarskog has a very detailed article for different methods:http://www.sommarskog.se/arrays-in-sql-2005.html
>
> And here is the performance analysis:http://www.sommarskog.se/arrays-in-sql-perftest.html
>
> HTH,
>
> Plamen Ratchevhttp://www.SQLStudio.com

In my experience the fastest method is parsing fixed width clobs/
blobs, not comma-separated ones. Unless, of course, it is a list of
VARCHAR with very different lengths...

 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 3) Posted: Tue May 13, 2008 2:31 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 13, 4:02 pm, "Aaron Bertrand [SQL Server MVP]"
wrote:
> > In my experience the fastest method is parsing fixed width clobs/
> > blobs, not comma-separated ones.
>
> But the latter is a much more popular format for incoming data. Do you have
> an easy / inexpensive routine that changes a comma-separated list to fixed
> width?

Unfortunately no I don't
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 4) Posted: Tue May 13, 2008 2:54 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

geoffrobinson ( ) writes:
> I've done a bunch of searches about the most efficient way to pull
> data out of delimited list, but most if not all of the results involve
> creating a delimited list. So I wanted to run this question by the
> group.
>
> If I have a delimited list along these lines:
> manhattan.nyc.ny.usa.world
>
> ...what would the best (or a good) way of getting just the second and
> third elements, for example, be?

SELECT str FROM iter_charlist_to_tbl(@list) WHERE listpos IN (2, 3)

http://www.sommarskog.se/arrays-in-sql-2005.html#iter-list-of-strings


--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 5) Posted: Tue May 13, 2008 3:13 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I believe this can be done with a Numbers helper table. I think it can also
be done with XML - and this may be your best option since you could possibly
use it to specify which elements you wanted back out. A web search should
be able to get you examples of both methods.

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"geoffrobinson" wrote in message

> Hi,
>
> I've done a bunch of searches about the most efficient way to pull
> data out of delimited list, but most if not all of the results involve
> creating a delimited list. So I wanted to run this question by the
> group.
>
> If I have a delimited list along these lines:
> manhattan.nyc.ny.usa.world
>
> ...what would the best (or a good) way of getting just the second and
> third elements, for example, be?
>
> At first I was thinking a loop with a cursor would be in order. But
> then my thoughts were turning to using Perl on the flat-files these
> were derived from. Also, using .NET in a stored procedure. But I would
> rather have a SQL solution to the problem.
>
> I appreciate any help in advance.
>
> thanks,
> Geoff
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
steve dassin

External


Since: Feb 04, 2008
Posts: 42



(Msg. 6) Posted: Tue May 13, 2008 4:06 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

It's 2008 and your looking for something as simple as splitting a string in
sql server. What's wrong with this picture?Smile Not much is really new.
Instead of thinking about what code to throw against the problem what about
thinking of what utility to use. This sql drudge work was worked out long
ago in the RAC utility. It's easy, powerful and doesn't really tax ones
brain cells to implement. If you're really interested in how RAC does it
I'll tell you. But if your first priority is getting it done as fast as
possible and as painlessly as possible you'll be rewarded Smile

www.rac4sql.net
See online documentation for splitting strings. There are plenty of
examples.

For a relational system that understands the net split operator:
www.beyondsql.blogspot.com

"geoffrobinson" wrote in message

> Hi,
>
> I've done a bunch of searches about the most efficient way to pull
> data out of delimited list, but most if not all of the results involve
> creating a delimited list. So I wanted to run this question by the
> group.
>
> If I have a delimited list along these lines:
> manhattan.nyc.ny.usa.world
>
> ...what would the best (or a good) way of getting just the second and
> third elements, for example, be?
>
> At first I was thinking a loop with a cursor would be in order. But
> then my thoughts were turning to using Perl on the flat-files these
> were derived from. Also, using .NET in a stored procedure. But I would
> rather have a SQL solution to the problem.
>
> I appreciate any help in advance.
>
> thanks,
> Geoff
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 7) Posted: Tue May 13, 2008 4:09 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Using CLR will give you very good performance on this type of split.

Erland Sommarskog has a very detailed article for different methods:
http://www.sommarskog.se/arrays-in-sql-2005.html

And here is the performance analysis:
http://www.sommarskog.se/arrays-in-sql-perftest.html

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 8) Posted: Tue May 13, 2008 5:02 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> In my experience the fastest method is parsing fixed width clobs/
> blobs, not comma-separated ones.

But the latter is a much more popular format for incoming data. Do you have
an easy / inexpensive routine that changes a comma-separated list to fixed
width?
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 9) Posted: Tue May 13, 2008 5:34 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This case seems good for CLR because of the requirement to return selected
items from the list based on position. Very easy to accomplish in .NET.

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 10) Posted: Thu May 15, 2008 9:55 am
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 13, 6:06 pm, "steve dassin" wrote:
> It's 2008 and your looking for something as simple as splitting a string in
> sql server. What's wrong with this picture?Smile Not much is really new.
> Instead of thinking about what code to throw against the problem what about
> thinking of what utility to use. This sql drudge work was worked out long
> ago in the RAC utility. It's easy, powerful and doesn't really tax ones
> brain cells to implement. If you're really interested in how RAC does it
> I'll tell you. But if your first priority is getting it done as fast as
> possible and as painlessly as possible you'll be rewarded Smile
>
> www.rac4sql.net
> See online documentation for splitting strings. There are plenty of
> examples.
>
> For a relational system that understands the net split operator:www.beyondsql.blogspot.com
>
> "geoffrobinson" wrote in message
>

Or one could just use table valued parameters, available in 2008.
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 11) Posted: Thu May 15, 2008 12:02 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> It's 2008 and your looking for something as simple as splitting a string
> in sql server. What's wrong with this picture?

Steve, the age of SQL Server has very little bearing on the experience of
the OP. You were a newbie too once, you know!

> This sql drudge work was worked out long ago in the RAC utility.

Sell! Sell! Buying a utility is not necessarily the only or best way to
solve a classic problem.
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
steve dassin

External


Since: Jan 14, 2008
Posts: 132



(Msg. 12) Posted: Thu May 15, 2008 8:35 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Aaron Bertrand [SQL Server MVP]" wrote in message

>> It's 2008 and your looking for something as simple as splitting a string
>> in sql server. What's wrong with this picture?
>
> Steve, the age of SQL Server has very little bearing on the experience of
> the OP. You were a newbie too once, you know!

I agree. I hope others bear this in mind too, then at least I would suffer
less wounds from the slings and arrows heaped upon me. Smile

>> This sql drudge work was worked out long ago in the RAC utility.
>
> Sell! Sell! Buying a utility is not necessarily the only or best way to
> solve a classic problem.
I bet MS understands someone who is on message. Think of your anticipation
of meeting me at the next MVP summit if MS decides to keep me closer than
its evangelists Smile

www.beyondsql.blogspot.com
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
steve dassin

External


Since: Jan 14, 2008
Posts: 132



(Msg. 13) Posted: Thu May 15, 2008 8:42 pm
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Alex Kuznetsov" wrote in message

>.
> Or one could just use table valued parameters, available in 2008.

Thank you for bringing this up. It is a subject that, I assure you, I will
visit forcefully Smile
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
geoffrobinson

External


Since: May 13, 2008
Posts: 2



(Msg. 14) Posted: Fri May 16, 2008 8:38 am
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 13, 3:46 pm, geoffrobinson wrote:
> Hi,
>
> I've done a bunch of searches about the most efficient way to pull
> data out of delimited list, but most if not all of the results involve
> creating a delimited list. So I wanted to run this question by the
> group.
>
> If I have a delimited list along these lines:
Just as an FYI, I went with the CLR route. Easy as pie. At least with
Visual Studio Professional. The functionality of having a SQL Server
project is missing from express. That would be much more of a bear.
The one link (http://www.sommarskog.se/arrays-in-sql-2005.html) didn't
know how to accomplish the CLR functionality with Visual Studio. But,
again, really easy with Professional (or Enterprise I guess).

Thanks for everyone's input.

> manhattan.nyc.ny.usa.world
>
> ...what would the best (or a good) way of getting just the second and
> third elements, for example, be?
>
> At first I was thinking a loop with a cursor would be in order. But
> then my thoughts were turning to using Perl on the flat-files these
> were derived from. Also, using .NET in a stored procedure. But I would
> rather have a SQL solution to the problem.
>
> I appreciate any help in advance.
>
> thanks,
> Geoff
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 15) Posted: Fri May 16, 2008 11:36 am
Post subject: Re: Getting Data Out of Delimited List [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Let me cut&paste an old method that uses a comma as a delimter

CREATE TABLE InputStrings
(keycol CHAR(10) NOT NULL PRIMARY KEY,
input_string VARCHAR(255) NOT NULL);

INSERT INTO InputStrings VALUES ('first', '12,34,567,896');
INSERT INTO InputStrings VALUES ('second', '312,534,997,896');
etc.

This will be the table that gets the outputs, in the form of the
original key column and one parameter per row.

It makes life easier if the lists in the input strings start and end
with a comma. You will need a table of sequential numbers -- a
standard SQL programming trick, Now, the query,

CREATE VIEW ParmList (keycol, place, parm)
AS
SELECT keycol,
COUNT(S2.seq), -- reverse order
SUBSTRING (I1.input_string
FROM S1.seq
FOR MIN(S2.seq) - S1.seq -1)
FROM InputStrings AS I1, Sequence AS S1, Sequence AS S2
WHERE SUBSTRING (',' + I1.input_string + ',', S1.seq, 1) = ','
AND SUBSTRING (',' + I1.input_string + ',', S2.seq, 1) = ','
AND S1.seq < S2.seq
GROUP BY I1.keycol, I1.input_string, S1.seq;

The S1 and S2 copies of Sequence are used to locate bracketing pairs
of commas, and the entire set of substrings located between them is
extracted and cast as integers in one non-procedural step. The trick
is to be sure that the right hand comma of the bracketing pair is the
closest one to the first comma. The relative position of each element
in the list is given by the value of "place", but it does a count down
so you can plan horizontal placement in columns.

Hey, I can write kludges with the best of them, but I don't. You need
to at the very least write a routine to clean out blanks, handle
double commas etc. Basically, you must write part of a compiler in
SQL. Yeeeech! Or decide that you do not want to have data integrity,
which is what most Newbies do in practice altho they do not know it.

This is a job for the front end, which should be passing scalar values
to the RDBMS.
 >> Stay informed about: Getting Data Out of Delimited List 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Command line tool for dumping (meta) data from tables to t.. - Question #1: We use sqlcmd in our installation procedure to do a lot of bulk inserts from tab deliminted files. Sometimes I need to dump an entire table from a development system to a tab delimited flat file to update the installation procedure. Is..

Which table holds data from Calendar list - I've created a Calendar in my SharePoint. When I open up SQL Server 2005 Management Studio, I can't find the table that holds all of the entered events. Any suggestions for where to look? Thanks

Delimited Seperators - Hi, I'm not real sure this is the right group to send to but I'll start here. I"m looking for some opinions on delimited seperators. What I'm trying to do is get data from sql and put it into a flat file and use a seperator between each column of...

Sending comma-delimited parameter to SP - My SP uses the IN operator, and I want to send this SP a comma-delimited string that will then be used with this IN operator. if I call it this SP this way " usp_names 'John, Bob' " it doesn't return anything. If I call it in this format &qu...

How to concatenating from rows into string with delimited .. - as subject
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Goto page 1, 2, 3
Page 1 of 3

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]