bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL References
SQL•SQL References

SQL Server CHARINDEX() Function

Concept visual

SQL Server CHARINDEX() Function

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Example

Search for "t" in string "Customer", and return position:

Select

CHARINDEX('t', 'Customer') AS MatchPosition;

Definition and Usage

The CHARINDEX() function searches for a substring in a string, and returns the position. If the substring is not found, this function returns 0.

Note:

Formula

This function performs a case - insensitive search.

Syntax

Charindex(

substring, string, start )

Parameter Values

Parameter

Description substring

Required. The substring to search for string Required. The string to be searched start Optional. The position where the search will start (if you do not want to start at the beginning of s tring ).

The first position in string is 1

Technical Details

Works in:

SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse

More Examples

Example

Search for "OM" in string "Customer", and return position:

Select

CHARINDEX('OM', 'Customer') AS MatchPosition;

Example

Search for "mer" in string "Customer", and return position (start in position
3):
SELECT CHARINDEX('mer', 'Customer', 3) AS MatchPosition;

Previous

❮ SQL Server Functions

Next

Previous

SQL Server CHAR() Function

Next

SQL Server CONCAT() Function