|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
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
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:58 AM
Points: 3,191,
Visits: 4,149
|
|
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);
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, March 29, 2013 1:52 AM
Points: 118,
Visits: 166
|
|
Got it right...
It took me 15 min to read and 15 second to answer
Prashant Bhatt Sr Engineer - Application Programming
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 2,551,
Visits: 18,884
|
|
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."
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 6:28 AM
Points: 1,258,
Visits: 4,259
|
|
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!
|
|
|
|
|
SSC-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.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, May 09, 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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:42 AM
Points: 1,072,
Visits: 1,026
|
|
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
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 5:54 PM
Points: 32,907,
Visits: 26,796
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|