The CHARINDEX function returns the starting position of a character or string in another string. The CHARINDEX function calling method is as follows:
CHARINDEX (expression1, expression2 [, start_location])
Expression1 is the character to be found in expression2, start_location is the start of the CHARINDEX function Find the position of expression1 in expression2.
The CHARINDEX function returns an integer. The returned integer is the position of the string to be found in the string being found. If CHARINDEX does not find the string it is looking for, then the function integer "0". Let's take a look at the results of the execution of the following function command:
CHARINDEX('SQL', 'Microsoft SQL Server')
This function command will return the "SQL" in "Microsoft SQL Server" ” starting position, in this example, the CHARINDEX function will return the position 11 of “S” in “Microsoft SQL Server”. Next, let’s look at this CHARINDEX command:
CHARINDEX('2008', 'Microsoft SQL Server 2012')
In this example, CHARINDEX returns zero because the string "2008" cannot Found in "Microsoft SQL Server". Next, let's take a look at how to use the CHARINDEX function to solve actual T-SQL problems through two examples.
The above is the detailed content of Explain the CHARINDEX function in SQL. For more information, please follow other related articles on the PHP Chinese website!