SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Split a String


Split a String

Author
Message
Lowell
Lowell
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67984 Visits: 40898
ChrisM@Work (3/12/2013)
AndrewSQLDBA (3/12/2013)
I was actually looking for something else. That really did not help.

Andrew SQLDBA


What's the problem with it, Andrew? There is an alternative; I'm just curious.


the default DelimitedSplit8K is limited to a single char delimiter, which is one tripwire, since he's got dbl pipes for the delimiter. so he'd have to adapt it, or find-and-replace the dbl piple with a single char, and either solution might not be obvious.

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16885 Visits: 6431
Many ways to skin this cat:


WITH SampleData AS (
SELECT val=REPLACE(val, '.', '#')
FROM (VALUES ('10.0||14.5'), ('2||34'), ('7.1||19'), ('4||11.7') ) v(val))
SELECT C1=REPLACE(PARSENAME(REPLACE(val, '||', '.'), 2), '#', '.')
,C2=REPLACE(PARSENAME(REPLACE(val, '||', '.'), 1), '#', '.')
FROM SampleData;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Steven Willis
Steven Willis
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1911 Visits: 1721
Lowell (3/12/2013)
...the default DelimitedSplit8K is limited to a single char delimiter, which is one tripwire, since he's got dbl pipes for the delimiter. so he'd have to adapt it, or find-and-replace the dbl piple with a single char, and either solution might not be obvious.

That statement is generally true, but in this case he still has just a single pipe character that can be used to split the string with DelimitedSplit8K. So no replaces necessary at all...

[EDIT: I see Eugene posted basically the same thing above. Great minds think alike! :-) ]



--just to keep the examples simple
--I'm using a temp table for the sample data

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable

CREATE TABLE #TempTable (
[Val] NVARCHAR(50) NULL)
INSERT INTO #TempTable
SELECT '10.0||14.5' UNION ALL
SELECT '2||34' UNION ALL
SELECT '7.1||19' UNION ALL
SELECT '4||11.7'




If you just need all the values in a single column how simple can it get?
DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?



SELECT
dsk.Item
FROM
#TempTable AS sd
CROSS APPLY
dbo.DelimitedSplit8K(sd.val,'|') AS dsk
WHERE
ItemNumber IN (1,3)

/*
Item
10.0
14.5
2
34
7.1
19
4
11.7
*/



And if you need to keep the pairs in their own columns just add another CROSS APPLY.
Still no replaces or string manipulation is necessary...DelimitedSplit8K will do its magic!



SELECT
dsk1.Item AS FirstVal
,dsk2.Item AS SecondVal
FROM
#TempTable AS sd
CROSS APPLY
dbo.DelimitedSplit8K(sd.val,'|') AS dsk1
CROSS APPLY
dbo.DelimitedSplit8K(sd.val,'|') AS dsk2
WHERE
dsk1.ItemNumber = 1
AND dsk2.ItemNumber = 3


/*
FirstVal SecondVal
10.0 14.5
2 34
7.1 19
4 11.7
*/



ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39474 Visits: 19991
Steven Willis (3/13/2013)
DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?


+1 ;-)

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
wolfkillj
wolfkillj
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2584 Visits: 2582
ChrisM@Work (3/14/2013)
Steven Willis (3/13/2013)
DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?


+1 ;-)


Well, if you only have a couple dozen nails to drive and you don't have the correct size in a strip or coil that fits your nail gun and your air compressor is in the garage buried under six lawn chairs, two coolers, a weed eater, and a couple of pairs of roller blades and you'd need to borrow or buy an additional 50' of air hose to reach your work area, the hammer may be the better choice!

The OP mentioned that he only needed to do a one-time job on about 100,000 rows. It seems much more expedient to write the CHARINDEX code and hit "execute" than to create the function and then write a query that calls it.

On the other hand, if I didn't already HAVE the nail gun and compressor, a project that involves driving a few dozen nails would make a pretty good justification for acquiring them! :-D The OP might be well-served in the future by having the DelimitedSplit8K function ready to go when he is tasked with a tougher string-split operation, i.e., the time it would take to create it now will likely pay off in the future if there is much string-splitting to be done in his shop..

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search