Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

CHARINDEX

Example

Find the position of the character “t” in the string “Customer” and return its index.

SELECT CHARINDEX(‘t’‘Customer’AS MatchPosition;

Definition and Usage

The CHARINDEX() function searches for a substring within a string and returns its position.

If the substring is not found, the function returns 0.

Importantly, this function conducts a case-insensitive search.

Syntax

CHARINDEX(substringstringstart)

Parameter Values

Parameter

Description

Substring

Mandatory. Specifies the substring to be searched for.

String

Mandatory. Specifies the string in which to search.

start

Optional. Specifies the position to begin the search (if you do not want to start at the beginning of the string). The first position in the string is 1.

Technical Details

Works in:

 SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Find the position of “OM” in the string “Customer” and return its index.

SELECT CHARINDEX(‘OM’‘Customer’AS MatchPosition;

Example

Find the position of “mer” in the string “Customer,” starting the search from position 3, and return its index.

SELECT CHARINDEX(‘mer’‘Customer’3AS MatchPosition;