Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Split a String Expand / Collapse
Author
Message
Posted Tuesday, March 12, 2013 12:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:08 AM
Points: 12,903, Visits: 32,146
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1430042
Posted Wednesday, March 13, 2013 8:47 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
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!
Post #1430716
Posted Wednesday, March 13, 2013 9:59 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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
*/


Post #1430732
Posted Thursday, March 14, 2013 1:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 AM
Points: 6,751, Visits: 13,899
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
Post #1430794
Posted Thursday, March 14, 2013 10:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 9:06 AM
Points: 1,061, Visits: 2,565
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! 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
Post #1431102
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse