The CHARINDEX function returns the starting position of a character or string in another string. The CHARINDEX function is called as follows:
CHARINDEX (expression1, expression2 [, start_location])
Expression1 is the character to be searched in expression2. start_location is the position where the CHARINDEX function starts to find expression1 in expression2.
The CHARINDEX function returns an integer that is the position of the string to be searched in the string to be searched. If CHARINDEX does not find the string to be searched, the function integer is "0 ". Let's take a look at the following function command execution results:
CHARINDEX ('SQL', 'Microsoft SQL Server ')
This function command returns the starting position of "SQL" in "Microsoft SQL Server". In this example, the CHARINDEX function returns position 11 of "S" in "Microsoft SQL Server.
Next, let's look at the CHARINDEX command:
CHARINDEX ('7. 0', 'Microsoft SQL Server 100 ')
In this example, CHARINDEX returns zero because the string "7.0" cannot be found in "Microsoft SQL Server. Next we will take two examples to see how to use the CHARINDEX function to solve the actual T-SQL problem.