Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the usage of patindex function in SQL

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Returns the position of the first occurrence of the pattern string in the expression expression, starting with 1.

The pattern string returns 0 without finding it in the expression expression, which is a valid data type for all valid text and strings.

Describe the specific use of this function:

1. PATINDEX ('% pattern%', expression)

The use of'% pattern%''is similar to that of like'% pattern%', which is to vaguely find whether its pattern string is found by expression, find it, and return its first occurrence.

Such as:

Select patindex ('% abb%','abcaabbeeabb')

Result 5, which is where abb first appeared.

2.PATINDEX ('% pattern', expression)

The use of'% pattern''is similar to the use of like'% pattern', preceded by a fuzzy lookup, that is, finding the location of the expression where the end of the pattern is located, that is, starting from the back match.

Such as:

Select patindex ('% abb','abcaabbeeabb')

Return 10, which is where abb first appeared later.

Select patindex ('% abb','abcaabbeeabbr')

Returns 0, and the first letter r after it does not match abb, so it returns 0

3.PATINDEX ('pattern%', expression)

The 'pattern%'' is similar to like 'pattern%', which is preceded by a precise search and followed by a fuzzy search, which is equivalent to querying the location of the pattern for the first time.

Such as:

Select patindex ('abb%','abbcaabbeeabb')

Return 1, which is equivalent to the starting value

Select patindex ('abb%','arbbcaabbeeabb')

Return 0, return 0 if you can't find it at first, no matter how many there are later.

4.PATINDEX ('pattern', expression)

This is equivalent to exact matching lookup, that is, pattern,expression is exactly equal.

Such as:

Select patindex ('abb','abb')

Return 1, exactly equal

Select patindex ('abb','abbr')

Returns 0, not exactly equal

5. Patindex ('% [pattern]%', 'ddabcaabbeeabbr')

Patindex ('% [^ pattern]%', 'ddabcaabbeeabbr')

Let's start with the use of [], which specifies some special characters. [^] A string other than [], every one of them.

Such as:

Symbol Meaning

LIKE'5 [%]'5%

LIKE'[_] n'_ n

LIKE'[a-cdf]'a, b, c, d, or f

LIKE'[- acdf]'-, a, c, d, or f

LIKE'[]'[

LIKE']']

LIKE 'abc [_] d%' abc_d and abc_de

LIKE 'abc [def]' abcd, abce, and abcf

Such as:

Select patindex ('% [d]%', 'rcrdsddddaadadffdr')

Returns the position of d in [] at the first occurrence of the string rcrdsddddaadadffdr.

Select patindex ('% [cd]%', 'rcrdsdddrdaadadffdr')

Return to 2, the position of cMagne d in [], return to the position where c first appeared, c in this string is 2, and d is 4, and the result is the first one.

Select patindex ('% [sd]%', 'rcrdsdddrdaadadffdr')

Return 4, the position of cMagne d in [], returns the position where s first appeared, s in this string is 5, and d is 4, and the result is the first one.

Select patindex ('% [^ r]%', 'rrrdsdddrdaadadffdr')

Return 4, except for the matching character of the string in [], the first occurrence of d is not in [^ r], so the first time this bit is found.

Select patindex ('% [^ rd]%', 'rrrdsdddrdaadadffdr')

Return 5, except for the matching character of the string in [], s is not in [^ rd] for the first time, so find this bit for the first time.

Select patindex ('% [^ rsd]%', 'rrrdsdddrdaadadffdr')

Return 11, except for the matching character of the string in [], the first occurrence of an is not in [^ rsd], so the first time this bit is found.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report