简体   繁体   English

SQL Server 中的 Charindex() 功能

[英]Charindex() functionality in SQL Server

Can anyone explain the actual working of CharIndex() function in SQL Server.任何人都可以解释 SQL Server 中 CharIndex() 函数的实际工作。 I have gone through MSDN and other websites.我浏览了 MSDN 和其他网站。 But I got few doubts after reading those.但读完这些后,我几乎没有怀疑。 I am able to understand with 2 parameters.我可以用 2 个参数来理解。 But I am not able to understand output once we use the third parameter(Start position)Please see the below examples.但是一旦我们使用第三个参数(起始位置)我就无法理解输出请看下面的例子。

SELECT CHARINDEX('t', 'TechOnTheNet.com', 3);
-- Result: 7

SELECT CHARINDEX('t', 'TechOnTheNet.com', 8);
-- Result: 12

I am not able to understand why the results like that for the above two.我无法理解为什么上述两个结果如此。

From CHARIDNEX来自CHARIDNEX

start_location起始位置

Is an integer or bigint expression at which the search starts.是搜索开始的整数或 bigint 表达式。 If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.如果未指定 start_location、为负数或为 0,则搜索从 expressionToSearch 的开头开始。

The searching is from ^ symbol forward.搜索是从^符号向前。

SELECT CHARINDEX('t', 'TechOnTheNet.com', 3); -- Result: 7
                         ^---x-------->   

SELECT CHARINDEX('t', 'TechOnTheNet.com', 8); -- Result: 12
                              ^---x--->

The 3rd parameter is the starting position, so any matching patterns prior to this point are skipped.第三个参数是起始位置,因此跳过此点之前的任何匹配模式。

So in your examples:-所以在你的例子中:-

SELECT CHARINDEX('t', 'TechOnTheNet.com', 3)

Starting position is 3, so the first 't' is skipped and returns the next one starting at position 7.起始位置为 3,因此跳过第一个 't' 并返回从位置 7 开始的下一个。

SELECT CHARINDEX('t', 'TechOnTheNet.com', 8)

Starting position is 8, so the first and second instances of 't' are skipped and returns the next one starting at position 12.起始位置为 8,因此跳过 't' 的第一个和第二个实例,并从位置 12 开始返回下一个实例。

The third argument (optional) to charindex is the start position for the search. charindex的第三个参数(可选)是搜索的开始位置。

SELECT CHARINDEX('t', 'TechOnTheNet.com', 3); -- Result: 7

In this case, the search for t starts at 3. But the output will be the position of t in the original string.在这种情况下, t的搜索从 3 开始。但输出将是t在原始字符串中的位置。

You can see it is similar for your other example as well.您可以看到它也与您的其他示例类似。

The last parameter is the start index, so that indicates the first string position where it will look for the specified character.最后一个参数是起始索引,因此它指示它将查找指定字符的第一个字符串位置。 The output is the absolute offset (one-based) of the next occurrence of the specified character.输出是指定字符下一次出现的绝对偏移量(基于一个)。

In your example there is a T character at offsets 1, 7, and 12 (it's case-insensitive).在您的示例中,偏移量 1、7 和 12 处有一个 T 字符(不区分大小写)。 The function returns the first occurrence greater than or equal to the specified start offset.该函数返回大于或等于指定起始偏移量的第一次出现。

You mentioned that you have gone through the MSDN article then i'm really clueless why you just missed this CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] ) part of that article.你提到你已经阅读了MSDN文章,然后我真的不知道你为什么错过了那篇文章的这个CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )部分。

it clearly said everything you want to know here.它清楚地说明了您想知道的一切。

CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] 

expressionToFind表达式查找

Is a character expression that contains the sequence to be found. expressionToFind is limited to 8000 characters.

expressionToSearch表达式搜索

Is a character expression to be searched.

start_location起始位置

 Is an integer or bigint expression at which the search starts.
 If start_location is not specified(It's OPTIONAL PARAMETER),
 is a negative number, or is 0,
 the search starts at the beginning of expressionToSearch.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM