• michael.kaufmann (7/13/2010)


    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?

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

    That's the point I don't get--when counting the delimiters of the @text variable, I'll only come up with 12.

    What am I missing here?

    Thanks

    Both the text before the first delimiter and the text after the last delimiter will be a row, as each bit of text between two delimiters. That results in #rows = #delimiters + 1.

    You can see this in the WHERE clause:

    WHERE (...) = @StringDelimiter -- Twelve rows, for the twelve delimiters.

    OR Number - 1 = LEN(@Text) -- One row, for the end of the text.

    If the last character is a delimiter (that is, if the text had ended in a dot instead of a question mark), the number of rows would be equal to the number of delimiters, with the "last" row matching both of the OR'ed conditions.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/