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 ««12345»»»

Split string using Tally Table Expand / Collapse
Author
Message
Posted Friday, July 2, 2010 4:28 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 5,925, Visits: 8,172
jts_2003 (7/2/2010)
I think this is an example of why people don't like to use OVER, since it's so hard to work out what might/will be returned!
I'd like to see a simpler question or articles on how OVER works - any takers?

Don't worry too much about the use of OVER in this question.
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), when used without FROM clause, is nothing but a contrived and needlessly complex synonym for SELECT 1.

The pain in this example is the hideously complex string handling functions in the SELECT clause. I didn't even TRY to work it out. Maybe if the author posts the code in a copy/pasteable format, I might be tempted to reformat until I see how the parentheses align, and then work out the details - but even then, I doubt if it'll be worth my time.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #946700
Posted Friday, July 2, 2010 5:30 AM
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: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Here is the formatted (in the way I like) code:

DECLARE @Text NVARCHAR(500)
DECLARE @StringDelimiter CHAR

SELECT @Text = 'This t-sql will split these sentences into rows.' +
'How many rows will be returned?.' +
'M.a.y.b.e..n.o.n.e.?',
@StringDelimiter = '.'

;WITH Tally(Number) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number
UNION ALL
SELECT Number + 1 AS Number
FROM Tally
WHERE Number <= LEN(@Text)
)
SELECT
CASE
WHEN RIGHT
( LEFT(@Text, Number),
CASE
WHEN CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0) > 0
THEN CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0) - 1
ELSE CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0)
END
) = ''
AND
CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1))) = 0
THEN LEFT (@Text, Number - 1)
ELSE
RIGHT
( LEFT(@Text, Number - 1),
CASE
WHEN CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0) > 0
THEN CHARINDEX(@StringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0) - 1
ELSE CHARINDEX(@STringDelimiter, REVERSE(LEFT(@Text, Number - 1)), 0)
END
)
END AS SPLIT
FROM Tally
WHERE (NCHAR(UNICODE(SUBSTRING(@Text, Number, 1))) = @StringDelimiter
OR Number - 1 = LEN(@Text))
OPTION(MAXRECURSION 32767);

Post #946723
Posted Friday, July 2, 2010 6:33 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 18, 2014 10:35 AM
Points: 118, Visits: 173
Got it right...

It took me 15 min to read and 15 second to answer


Prashant Bhatt
Sr Engineer - Application Programming
Post #946753
Posted Friday, July 2, 2010 6:42 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 25, 2014 6:31 AM
Points: 2,656, Visits: 19,187
Hugo, thanks again for the excellent re-write and explanation.

Not sure why this one is tripping folks up, if you just count the delimiters, you can see it will return 13 pieces?

...anyway...good question, I'm sure it's introduced some folks to this idea that haven't seen it before.


---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Post #946764
Posted Friday, July 2, 2010 6:49 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:12 AM
Points: 1,610, Visits: 5,482
jcrawf02 (7/2/2010)
Not sure why this one is tripping folks up, if you just count the delimiters, you can see it will return 13 pieces?


As I said, I thought it was a trick question and the code as presented simply wouldn't work for some reason!
Post #946777
Posted Friday, July 2, 2010 7:50 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 22, 2010 8:59 AM
Points: 110, Visits: 952
jcrawf02 (7/2/2010)
Hugo, thanks again for the excellent re-write and explanation.

Not sure why this one is tripping folks up, if you just count the delimiters, you can see it will return 13 pieces?

...anyway...good question, I'm sure it's introduced some folks to this idea that haven't seen it before.


I assumed the extra crazy in string handling was doing something to consume the double/adjacent delimiter, else this was "simply" a string split using Tally. The question was so easy I assumed it was a trick.
Post #946836
Posted Friday, July 2, 2010 8:18 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 9, 2013 9:59 AM
Points: 236, Visits: 149
Hello all and thanks for the time spend on answering my posted question.

Originally i came across with a similar t-sql already posted by Hugo. That's definitely the simplest and quickest way of doing split string using tally. Thanks Hugo.

Nevertheless i wanted to make sure i could get the same result by using some string functions.
Sorry about the messy code... I should have submitted correctly formatted. Consider it as an extra level of difficulty.

Anyway, as almost of you got it, the catch was to look to where clause...

My major concern was to post an example of using tally. Back a few months ago i read some articles discussing tally and how to replace cursors and while loops with it and become a huge fan since then.

I know it isn't the best example, but got you guys thinking on it.

Best Regards,

PM

Post #946852
Posted Friday, July 2, 2010 8:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 1:04 PM
Points: 1,313, Visits: 1,055
For a SQL Server 2000 (and prior) dinosaur like myself, the whole concept of defining a Common Table Expresssion (CTE), never mind a recursive one, was new to me.

If anyone else is in the same boat, I found the following article very helpful:

http://www.4guysfromrolla.com/webtech/071906-1.shtml

Post #946872
Posted Friday, July 2, 2010 8:57 AM
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: Thursday, July 17, 2014 10:56 AM
Points: 3,924, Visits: 1,607
Thanks Hogo for explanation.

I was completely lost in question and does not even know what to answer. A brain teaser with small query is always good but with this big query that to in a image file is a brain torture.

Well, I got right but just by fluke. No efforts on trying to find, why I was right.


SQL DBA.
Post #946895
Posted Friday, July 2, 2010 11:23 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:48 AM
Points: 36,759, Visits: 31,214
BWAA-HAA!!!! First, this is NOT a good example of Tally Table code. It doesn't use a Tally Table and it doesn't use anything that could be called efficient. Anyone who uses a recursive CTE to generate Tally numbers just doesn't know what hidden RBAR is.

I agree with what someone else has stated... this code should be used as an example of how NOT to write code and how NOT to accomplish a split. Same goes for any example in this thread that uses a recursive CTE to do the split.

I hope no one actually copies the code to use as a split function but I will say it was very interesting in how many "extra" things were added to the code in an attempt at obfuscation. Heh... maybe it should be titled "job security methods".


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #947022
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse