Desculpa pessoal chegar tão tarde, mas eu gostaria de facilitar as coisas para as pessoas que querem expandir isso. Eu estava olhando para cada uma dessas implementações, peguei a que me pareceu melhor (Aaron Bertrand), simplifiquei e aqui está, você tem o "modelo". Use com sabedoria.
CREATE FUNCTION dbo.CHARINDICES (
@search_expression NVARCHAR(4000),
@expression_to_be_searched NVARCHAR(MAX)
) RETURNS TABLE AS RETURN (
WITH tally AS (
SELECT Number = ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_objects)
SELECT DISTINCT n = subIdx -- (4) if we don't perform distinct we'll get result for each searched substring, and we don't want that
FROM
tally
CROSS APPLY (SELECT subIdx = CHARINDEX(@search_expression, @expression_to_be_searched, Number)) x -- (2) subIdx is found in the rest of the substring
WHERE
Number BETWEEN 1 AND LEN(@expression_to_be_searched) -- (1) run for each substring once
AND SubIdx != 0 -- (3) we care only about the indexes we've found, 0 stands for "not found"
)
SELECT CHARINDEX('C', 'BACBABCBABBCBACBBABC')
SELECT * FROM dbo.CHARINDICES('C', 'BACBABCBABBCBACBBABC')
Apenas como referência - você pode derivar outros comportamentos disso, como expandir em PATINDEX ():
CREATE FUNCTION dbo.PATINDICES (
@search_expression NVARCHAR(4000) = '%[cS]%',
@expression_to_be_searched NVARCHAR(MAX) = 'W3Schools.com'
) RETURNS TABLE AS RETURN (
WITH tally AS (
SELECT num = ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_objects)
SELECT DISTINCT n = subIdx + num - 1
FROM
tally
CROSS APPLY (SELECT numRev = LEN(@expression_to_be_searched) - num + 1) x
CROSS APPLY (SELECT subExp = RIGHT(@expression_to_be_searched, numRev)) y
CROSS APPLY (SELECT subIdx = PATINDEX(@search_expression, subExp)) z
WHERE
num BETWEEN 1 AND LEN(@expression_to_be_searched)
AND SubIdx != 0
)
SELECT PATINDEX('%[cS]%', 'W3Schools.com')
SELECT * FROM dbo.PATINDICES('%[cS]%', 'W3Schools.com')
1
na string que contém apenas zeros e uns. Eu usei Sua solução e @ aaron-bertrand, mas obtive os mesmos resultados e o mesmo desempenho. Qual solução seria melhor?