本文共 10382 字,大约阅读时间需要 34 分钟。
We use the SQL CHARINDEX function to find the position of a substring or expression in a given string. We might have a character in different positions of a string. SQL CHARINDEX returns the first position and ignores the rest of matching character positions in a string.
我们使用SQL CHARINDEX函数查找给定字符串中子字符串或表达式的位置。 我们可能在字符串的不同位置有一个字符。 SQL CHARINDEX返回第一个位置,并忽略字符串中其余匹配字符的位置。
CHARINDEX ( expression_to_find , expression_to_search [ , start_location ] )
It takes following parameters in SQL CHARINDEX function.
它在SQL CHARINDEX函数中采用以下参数。
In this example, we want to find position of @ character in a specified email address rajendra.gupta16@gmail.com
在此示例中,我们要查找@字符在指定电子邮件地址rajendra.gupta16@gmail.com中的位置
In the following screenshot, we can see position of each character in the email address string. The character ‘@’ is in position 17. We get this position in output of SQL CHARINDEX.
在下面的屏幕截图中,我们可以看到每个字符在电子邮件地址字符串中的位置。 字符“ @”位于位置17。我们在SQL CHARINDEX的输出中获得此位置。
SELECT CHARINDEX ('@','rajendra.gupta16@gmail.com') as 'CharacterPosition'
Similarly, let’s search dot (.) position in this string. We can see that the dot is on position 9th and 23rd in the specified string (email address).
同样,让我们搜索该字符串中的点(。)位置。 我们可以看到该点在指定字符串(电子邮件地址)的第9和23rd位置上。
SELECT CHARINDEX('.','rajendra.gupta16@gmail.com',11) as 'CharacterPosition'
Once we execute this script, it returns the first position of the dot in the output. It starts the search from starting position of a string and stops once it finds a suitable match.
一旦执行了此脚本,它将在输出中返回点的第一个位置。 它从字符串的起始位置开始搜索,一旦找到合适的匹配项便停止搜索。
Suppose we want to get the position of the second dot (.) in this email address. We can specify a value for an optional parameter to start searching from a specific position. It starts from a specific character position and checks for the character position. We still get the actual position of the character that is 23rd in this example.
假设我们要获取此电子邮件地址中第二个点(。)的位置。 我们可以为可选参数指定一个值,以从特定位置开始搜索。 它从特定的字符位置开始,并检查字符位置。 在此示例中,我们仍然获得第23个字符的实际位置。
In previous examples, we searched a specific character in a specified string. We can also search a substring as well in a string.
在前面的示例中,我们在指定的字符串中搜索了特定字符。 我们还可以在字符串中搜索子字符串。
In the following query, we declared a variable @ExpressionToSearch for the string and set a value on it. We want to search for substring Rajendra in this string.
在以下查询中,我们为该字符串声明了一个变量@ExpressionToSearch并在其上设置了一个值。 我们要在此字符串中搜索子字符串Rajendra 。
DECLARE @ExpressionToSearch varchar(100)SET @ExpressionToSearch = 'Explore SQL Server on SQLShack with Rajendra Gupta articles'SELECT CHARINDEX ('Rajendra', @ExpressionToSearch) AS 'CharacterPosition'
It searches for the substring in a specified string. If it gets an exact match of the substring, it returns the starting position of the substring.
它在指定的字符串中搜索子字符串。 如果它与子字符串完全匹配,则返回子字符串的起始位置。
If an exact match is not found it returns 0 in the output.
如果未找到完全匹配,则在输出中返回0。
Suppose we want to search a substring in a specified string. In the string we have multiple matching substrings. For example, in the following query, we want to search for SQLShack and find its position.
假设我们要搜索指定字符串中的子字符串。 在字符串中,我们有多个匹配的子字符串。 例如,在以下查询中,我们要搜索SQLShack并找到其位置。
DECLARE @ExpressionToSearch varchar(100)SET @ExpressionToSearch = 'Explore SQL Server on SQLShack with Rajendra Gupta articles - SQLShack'SELECT CHARINDEX ('SQLShack', @ExpressionToSearch) AS 'CharacterPosition'
We can use start_location in SQL CHARINDEX with a substring as well. For example, let’s start with position 24 and see the result. It starts from character position 24 and searches for a particular substring. We can see the substring starting position is now at 63.
我们也可以在SQL CHARINDEX中将start_location与子字符串一起使用。 例如,让我们从位置24开始并查看结果。 它从字符位置24开始,并搜索特定的子字符串。 我们可以看到子字符串的起始位置现在是63。
DECLARE @ExpressionToSearch varchar(100)SET @ExpressionToSearch = 'Explore SQL Server on SQLShack with Rajendra Gupta articles - SQLShack'SELECT CHARINDEX ('SQLShack', @ExpressionToSearch) AS 'CharacterPosition'
We can use SQL CHARINDEX with SQL Case statement to search a particular substring existence in a specified string.
我们可以使用带有SQL Case语句SQL CHARINDEX来搜索指定字符串中特定子字符串的存在。
DECLARE @Name AS VARCHAR(100)= 'Explore SQL Server with articles on SQLShack';SELECT CASE WHEN CHARINDEX('SQLShack', @Name) > 0 THEN 'Exists' ELSE 'Not Exists' END AS FindSubString;SELECT CASE WHEN CHARINDEX('Rajendra', @Name) > 0 THEN 'Exists' ELSE 'Not Exists' END AS FindSubString;
In the following screenshot, we can see that SQL CHARINDEX function checks for a particular substring. If it returns a value greater than 0 it means substring exists in specified string else it does not exist.
在下面的屏幕截图中,我们可以看到SQL CHARINDEX函数检查特定的子字符串。 如果返回的值大于0,则表示指定的字符串中存在子字符串,否则不存在。
In the previous examples, we did not use case sensitive search. For example, in the following query, we want to search for substring sqlshack in our string. This substring exists does but it exists in upper case.
在前面的示例中,我们没有使用区分大小写的搜索。 例如,在以下查询中,我们要在字符串中搜索子字符串sqlshack 。 该子字符串确实存在,但以大写形式存在。
DECLARE @ExpressionToSearch varchar(100)SET @ExpressionToSearch = 'Explore SQL Server on SQLSHACK with Rajendra Gupta articles - SQLShack'SELECT CHARINDEX ('sqlshack', @ExpressionToSearch) AS 'CharacterPosition'
It does not perform case sensitive search, and we still get the correct output.
它不执行区分大小写的搜索,并且我们仍然获得正确的输出。
We can use collation to perform case sensitive search. In the following examples, we use COLLATE Latin1_General_CS_AS to perform case sensitive search. We need to note that all character case in a substring should match within a string. Execute the following query to understand this.
我们可以使用排序规则来执行区分大小写的搜索。 在以下示例中,我们使用COLLATE Latin1_General_CS_AS执行区分大小写的搜索。 我们需要注意,子字符串中的所有字符大小写都应在字符串中匹配。 执行以下查询以了解这一点。
DECLARE @ExpressionToSearch varchar(100)SET @ExpressionToSearch = 'Explore SQL Server on SQLSHACK with Rajendra Gupta articles 'SELECT CHARINDEX ('sqlshack', @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS 'CharacterPosition' SELECT CHARINDEX ('SQLShack', @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS 'CharacterPosition' SELECT CHARINDEX ('SQLSHACK', @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS 'CharacterPosition'
We can use SQL CHARINDEX for existing data in a table. We can use it to get output in a separate column. In the following example, we want to check the position of character R in empname column values of the Employee table.
我们可以对表中的现有数据使用SQL CHARINDEX。 我们可以使用它在单独的列中获取输出。 在下面的示例中,我们想检查字符R在Employee表的empname列值中的位置。
SELECT TOP 10 [EmpName], CHARINDEX('R', empname) AS "Position of R", [City], [Designation]FROM [SQLShackDemo].[dbo].[Employee];
In the following screenshot, we have a new column to get position of character R in EmpName column values. If Empname does not contain specified character R, it returns 0.
在以下屏幕截图中,我们有一个新列来获取字符R在EmpName列值中的位置。 如果Empname不包含指定的字符R,则返回0。
Let’s update one record in Employee table and replace empname with NULL.
让我们更新Employee表中的一条记录,并将empname替换为NULL。
Update [SQLShackDemo].[dbo].[Employee] set EmpName=NULL where empname='Charlotte Robinson'
Rerun the SQL CHARINDEX query, and we see value NULL in Position of R column. If we have a NULL value in a column, it also returns a NULL value.
重新运行SQL CHARINDEX查询,我们在R的“位置”列中看到值NULL。 如果我们在列中有一个NULL值,它也会返回一个NULL值。
We can search for numeric value as well as using SQL CHARINDEX. Suppose we want to find a position of character 1 in empid column of the employee table.
我们可以搜索数值,也可以使用SQL CHARINDEX。 假设我们要在employee表的empid列中找到字符1的位置。
SELECT TOP 10 [EmpName],EmpID, CHARINDEX('1', empid) AS "Position of 1", [City], [Designation]FROM [SQLShackDemo].[dbo].[Employee];
We need to specify numeric value as well in single quotes. If we do not put single quotes, it gives following error message.
我们还需要在单引号中指定数值。 如果我们不使用单引号,则会显示以下错误消息。
Msg 8116, Level 16, State 1, Line 2Argument data type int is invalid for argument 1 of charindex function.
In this article, we explored SQL CHARINDEX function and its usage with various examples. Please feel free to provide feedback or ask questions in the comment section below.
在本文中,我们通过各种示例探讨了SQL CHARINDEX函数及其用法。 请随时在下面的评论部分中提供反馈或提出问题。
翻译自:
转载地址:http://fznwd.baihongyu.com/