The SQL CHARINDEX function returns the starting position of a character expression in another character expression.
The CHARINDEX function helps with string data manipulation like some other SQL functions of SUBSTRING, REPLACE, CONCAT, etc. The CHARINDEX function finds the beginning of a character expression in a given character expression. The result of the CHARINDEX function is the index position.
SQL CHARINDEX Usage Notes
The first argument of the CHARINDEX function is the character expression or the substring whose position is to be found. The limit is 8000 characters.
The second argument of the CHARINDEX function is the character expression that is searched for the starting position of the first character expression.
The third argument is the starting position. If it is not specified or is a negative number, then the search begins from position 1. This argument helps when we do not want to start the search from the beginning. We can provide any starting position. The CHARINDEX function starts indexing from 1 instead of 0.
The CHRAINDEX function returns an integer value. The return value is bigint in case of nvarchar(max), varchar(max), or varbinary(max). The input character expression cannot be an image or a text data type. If the character expression is not present in the main expression, then a zero value returns. If any of the first two arguments are NULL, then a NULL value returns.
CHARINDEX can perform both case-sensitive and case-insensitive matching for the expression.
CHARINDEX (sub_expression, main_expression, start_position (optional))
SQL CHARINDEX Examples
Following is a simple example of using a CHARINDEX function in SQL.
/* Answer */ SELECT CHARINDEX('charindex', 'The CHARINDEX function', 4) position;
The CHARINDEX function searches the position of the ‘charindex’ expression and finds it although it is in the lower case. The starting position of 4 begins searching from position 4. The indexing remains the same, and we get position 5 as the starting index of the sub expression. The result is the same if we do not specify the start position.
Below, we will see how to perform a case-sensitive search using the CHARINDEX function.
/* Answer */ SELECT CHARINDEX('charindex', 'The CHARINDEX function' COLLATE Latin1_General_CS_AS) position;
The output is a zero value because ‘charindex’ is not present in the string expression in the lower case.
The following example uses the AdevntureWorks2019 database. The CHARINDEX function finds the starting position of skills information in the resume of a job candidate.
/* Answer */ SELECT TOP(3) CHARINDEX('Skills', CONVERT(varchar(8000), Resume)) As skills_position FROM HumanResources.JobCandidate;
The TOP(3) function selects the top three records in the Job Candidate table. Another point to note is the use of the convert function. As the data type of the Resume column is XML, the Convert function converts the data type from XML to varchar. After conversion, the CHARINDEX function searches for the expression ‘Skills’ in the resume data and returns its starting location. In this way, the CHARINDEX function helps to go through lengthy records and locate the specific data.