Interview Question of the Week #029 – Difference Between CHARINDEX vs PATINDEX

This interview question needs a short answer before the syntax: one function looks for literal text, and the other looks for a wildcard pattern. If the underscore is supposed to mean “any one character,” that difference matters immediately.

A narrow net catches one fish while a broad net catches several fish

Question: What is the difference between CHARINDEX and PATINDEX in SQL Server?

Answer: CHARINDEX searches for a literal substring and may start at a specified position. PATINDEX searches using the familiar LIKE wildcards, including % for any sequence and _ for one character. It has no separate start-position argument. Both return a one-based position, or zero when there is no match.

Here is a small example you can run without a sample database. The string contains important and impXrtant. A wildcard pattern matches the first form at position 4; a literal underscore is absent.

DECLARE @Text nvarchar(100) = N'An important item and an impXrtant item.';
SELECT CHARINDEX(N'important', @Text) AS LiteralMatch,
       CHARINDEX(N'important', @Text, 10) AS LaterLiteralMatch,
       PATINDEX(N'%imp_rtant%', @Text) AS WildcardMatch,
       CHARINDEX(N'imp_rtant', @Text) AS LiteralUnderscoreMatch;
Complete CHARINDEX and PATINDEX example with literal 4, later 0, wildcard 4 and literal-underscore 0
The query and its result: literal match 4, later literal 0, wildcard match 4 and absent literal underscore 0.

The same comparison works on real data. AdventureWorks2025 has a Production.Document table with text summaries:

USE AdventureWorks2025;
GO
SELECT DocumentSummary,
       CHARINDEX('important', DocumentSummary) AS LiteralMatch,
       PATINDEX('%imp_rtant%', DocumentSummary) AS PatternMatch
FROM Production.Document
WHERE Title = 'Repair and Service Guidelines'
  AND DocumentSummary IS NOT NULL;

Both functions return NULL when the searched text is NULL, so the filter keeps the result readable. Collation still affects case and accent comparisons. If the search value contains a literal percent or underscore, keep it literal with CHARINDEX or escape it deliberately in the pattern. Use the function that matches the question you actually mean to ask.

String search: CHARINDEX or PATINDEX?

Want to try this yourself? Here is how to install AdventureWorks.

CHARINDEX is not a weaker PATINDEX, it is the literal search, and the underscore is where the two part ways.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Previous Post
Interview Question of the Week #028 – Few SQL Best Practices
Next Post
Interview Question of the Week #030 – Retrieve Last Inserted Identity of Record

Related Posts

No results found.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.