Thank You Mister Magoo
That worked perfectly
Thanks for the suggestions everyone, just way overkill for what was actually needed.
Andrew SQLDBA
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
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
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
/*
FirstValSecondVal
10.014.5
234
7.119
411.7
*/
Steven Willis (3/13/2013)
DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?
+1
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
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
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply