• Hi

    It works like this. For two equal strings, say @mcode= '0011246';

    Split c.TheValues into chunks (tokens), and test each one to see if it appears anywhere in @mcode. The tokens in '0011246' are as follows:

    001

    011

    112

    124

    246

    ...because they must be three characters long. So the maximum number of distinct tokens which can match between two equal strings (a single token could match more than once) is @mcode-2.

    HTH

    ChrisM

    [font="Courier New"]SELECT @mcode, c.TheValues, SUBSTRING(c.TheValues, n.number, 3)

    FROM [Numbers] n

    INNER JOIN #Table c ON CHARINDEX(SUBSTRING(c.TheValues, n.number, 3), @mcode) > 0

    WHERE n.number <= LEN(@mcode) AND LEN(SUBSTRING(c.TheValues, n.number, 3)) = 3

    AND c.TheValues = '0011246'

    ORDER BY c.TheValues, SUBSTRING(c.TheValues, n.number, 3)[/font]

    “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