SQL CHARINDEX

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返回第一个位置,并忽略字符串中其余匹配字符的位置。

SQL CHARINDEX函数语法 (SQL CHARINDEX Function Syntax)

CHARINDEX ( expression_to_find , expression_to_search [ , start_location  ] )

It takes following parameters in SQL CHARINDEX function.

它在SQL CHARINDEX函数中采用以下参数。

  • expression_to_find: In this parameter, we specify a character or string that we want to search in another string expression_to_find :在此参数中,我们指定要在另一个字符串中搜索的字符或字符串
  • expression_to_search: We can specify a string or sentence in which we want to search expression_to_search:我们可以指定要在其中搜索expression_to_find expression_to_find的字符串或句子
  • start_location: It is an optional parameter. We can specify an integer value in this parameter to specify start location. If we want to search start_location:这是一个可选参数。 我们可以在此参数中指定一个整数值以指定开始位置。 如果要在expression_to_find in expression_to_search中搜索具有指定起始位置的expression_to_search with a specified start location, we can specify it. By default, if we do not mention any value in this parameter, it starts a search from the index position 0 expression_to_find ,则可以指定它。 默认情况下,如果我们在此参数中未提及任何值,它将从索引位置0开始搜索

示例1:在字符串中搜索字符位置 (Example 1: Search a character position in a string)

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'

SQL CHARINDEX examples

示例2:在SQL CHARINDEX中使用可选参数Start_position (Example 2: Use of optional parameter Start_position in SQL CHARINDEX)

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.

一旦执行了此脚本,它将在输出中返回点的第一个位置。 它从字符串的起始位置开始搜索,一旦找到合适的匹配项便停止搜索。

SQL CHARINDEX examples

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个字符的实际位置。

SQL Server CharIndex example

示例3:在SQL CHARINDEX中的指定字符串中搜索子字符串位置 (Example 3: Search a substring position in a specified string in SQL CHARINDEX)

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.

它在指定的字符串中搜索子字符串。 如果它与子字符串完全匹配,则返回子字符串的起始位置。

Search a substring position in a specified string

If an exact match is not found it returns 0 in the output.

如果未找到完全匹配,则在输出中返回0。

Search a substring position in a specified string

示例4:在SQL CHARINDEX中具有多个匹配项的指定字符串中搜索子字符串位置 (Example 4: Search a substring position in a specified string with multiple matching in SQL CHARINDEX)

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'

Search a substring position in a specified string with multiple matching

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'

Search a substring position in a specified string with multiple matching

示例5:带有SQL CASE语句SQL CHARINDEX (Example 5: SQL CHARINDEX with SQL CASE statement)

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,则表示指定的字符串中存在子字符串,否则不存在。

  • SQLShack exists in a specified string that’s why the output is Exists SQLShack存在于指定的字符串中,这就是为什么输出存在的原因
  • Rajendra does not exist in a specified string that’s why the output is Not Exists Rajendra在指定的字符串中不存在,这就是输出不存在的原因

SQL CASE statement

示例6:使用SQL CHARINDEX区分大小写的搜索 (Example 6: Case sensitive search with SQL CHARINDEX)

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.

它不执行区分大小写的搜索,并且我们仍然获得正确的输出。

SQL Server CASE statement

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'

Case Sensitive search in SQL Server

示例7:SQL CHARINDEX和表列 (Example 7: SQL CHARINDEX and table column)

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。

example of table column with SQL Server charindex

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值。

example of table column with SQL Server charindex

示例8:SQL CHARINDEX和数值 (Example 8: SQL CHARINDEX and Numeric value )

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];

Search Numeric value in a string

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 2
Argument data type int is invalid for argument 1 of charindex function.

SQL CHARINDEX快速回顾 (Quick Recap of SQL CHARINDEX)

  • SQL CHARINDEX Returns a position of a substring within a string

    SQL CHARINDEX返回字符串中子字符串的位置
  • Big Int value else it returns Big Int值,否则返回Int data type Int数据类型
  • By default, it performs a case insensitive search

    默认情况下,它执行不区分大小写的搜索
  • 0 in return 0

结论 (Conclusion)

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函数及其用法。 请随时在下面的评论部分中提供反馈或提出问题。

翻译自: https://www.sqlshack.com/sql-charindex/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值