简体   繁体   中英

Charindex() functionality in SQL Server

Can anyone explain the actual working of CharIndex() function in SQL Server. I have gone through MSDN and other websites. But I got few doubts after reading those. I am able to understand with 2 parameters. 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

start_location

Is an integer or bigint expression at which the search starts. If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of 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.

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.

The third argument (optional) to charindex is the start position for the search.

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.

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). 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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