The topic describes the DF042 T-SQL code analysis rule.
BEST PRACTICE
The CHARINDEX function is used in the SELECT, UPDATE, or DELETE statement.
Avoid using the CHARINDEX function in filtering clauses of the SELECT, UPDATE, and DELETE statements.
Using CHARINDEX in filtering clauses may lead to inefficient query execution, especially on large datasets. This is because CHARINDEX operates on each row individually, potentially resulting in poor performance due to excessive string manipulation.
SELECT CHARINDEX('bicycle', 'Reflectors are vital for bicycle safety.') AS char_index
FROM dbo.DemoTable
GO
DECLARE @char_index BIGINT;
SET @char_index = CHARINDEX('bicycle', 'Reflectors are vital for bicycle safety.');
SELECT @char_index AS char_index;
GO