Find the position of the character “t” in the string “Customer” and return its index.
SELECT CHARINDEX(‘t’, ‘Customer’) AS MatchPosition; |
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.
CHARINDEX(substring, string, start) |
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. |
Works in: |
SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse |
Find the position of “OM” in the string “Customer” and return its index.
SELECT CHARINDEX(‘OM’, ‘Customer’) AS MatchPosition; |
Find the position of “mer” in the string “Customer,” starting the search from position 3, and return its index.
SELECT CHARINDEX(‘mer’, ‘Customer’, 3) AS MatchPosition; |