In SQL Server, working with strings often requires finding whether a substring exists within a larger piece of text. One of the most efficient and commonly used functions for this purpose is the CHARINDEX()
function. This simple yet powerful function can help developers locate the position of a substring within a string, which can be essential for data filtering, validation, or transformation tasks.
Understanding how to properly use CHARINDEX()
can significantly improve the efficiency and readability of your SQL code. It is especially useful when handling tasks like parsing raw data, searching keywords in user input, or even cleaning up inconsistent entries.
What is CHARINDEX()
?
The CHARINDEX()
function returns the position (as an integer) of a specified substring within a larger string. If the substring is found, it returns the position of the first occurrence starting from 1. If it is not found, it returns 0.
Syntax:
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
- expressionToFind: The substring you’re searching for.
- expressionToSearch: The full string you want to search within.
- start_location (optional): The position in the main string at which the search will begin.
Basic Usage of CHARINDEX()
Here’s a simple example to find the position of the word “SQL” in a string:
SELECT CHARINDEX('SQL', 'Learn SQL with practice');
This will return 7, indicating that the substring “SQL” starts at the 7th position.

Using CHARINDEX()
with the Optional Start Position
Sometimes you don’t want to start at the beginning of the string. In such cases, you can provide the optional start_location argument. This is useful when you want to find the second or third occurrence of a substring.
SELECT CHARINDEX('is', 'This is interesting', 4);
In this case, the first “is” appears at position 3, but by starting at position 4, we skip that and find the second occurrence at position 6.
Case Sensitivity with CHARINDEX()
Whether CHARINDEX()
is case-sensitive depends on the collation settings of the SQL Server or the specific column being searched. For example, under a case-insensitive collation like SQL_Latin1_General_CP1_CI_AS, searching for ‘SQL’ or ‘sql’ yields the same result. In a case-sensitive collation, they would be treated differently.
Practical Scenario: Filtering Rows Based on Substring
One practical use case of CHARINDEX()
is when filtering table rows based on the value of a column that contains a certain word.
SELECT *
FROM Products
WHERE CHARINDEX('Apple', ProductName) > 0;
This query returns all products where the name includes the word “Apple”.
Combining CHARINDEX()
with Other Functions
Often, CHARINDEX()
is used alongside functions like SUBSTRING()
to extract a portion of a string. For example, extracting a domain name from an email address:
SELECT SUBSTRING(Email,
CHARINDEX('@', Email) + 1,
LEN(Email)) AS Domain
FROM Users;
This extracts the portion of the email address after the “@” symbol.
Advantages of Using CHARINDEX()
- Simple to use and understand.
- Supports optional start positions for flexible searches.
- Performs efficiently with string matching when used properly.
Limitations
- Cannot perform regular expression searches; only basic substring matches.
- Its case-sensitivity can vary based on collation, which may lead to unexpected results.
Conclusion
The CHARINDEX()
function is a valuable tool for any SQL developer dealing with string data. Whether you’re performing basic searches or combining it with other string functions to create powerful queries, mastering CHARINDEX()
will significantly enhance your SQL scripting capabilities.
FAQ
- Q: What does CHARINDEX() return if the substring is not found?
A: It returns 0. - Q: Can CHARINDEX() be used with columns in a WHERE clause?
A: Yes, it is often used to filter rows that contain specific substrings. - Q: Is CHARINDEX() case-sensitive?
A: It depends on the collation of the database or column. Case-insensitive collations treat ‘SQL’ and ‘sql’ as equal. - Q: How do I find the second occurrence of a substring?
A: Use the optional third argument to start the search just after the position of the first occurrence. - Q: What is the difference between CHARINDEX() and PATINDEX()?
A: CHARINDEX() looks for exact substrings, while PATINDEX() allows for wildcard pattern matching.