返回一个整数,该整数表示模式第一次在字符串中出现的起始位置。
PATINDEX( '%pattern%', string-expression )
pattern 要搜索的模式。如果省略前导百分号通配符,则模式出现在字符串起始处时,PATINDEX 函数返回一 (1),否则返回零。
UltraLite 的模式使用以下通配符:
通配符 | 匹配项 |
---|---|
_(下划线) | 任意一个字符 |
%(百分号) | 包含零个或多个字符的任意字符串 |
[] | 指定范围或集合中的任何单个字符 |
[^] | 不在指定范围或集合中的任何单个字符 |
string-expression 要在其中搜索模式的字符串。
INT
PATINDEX 函数返回模式第一次出现的开始位置。如果未找到模式,则返回零 (0)。
以下语句返回值 2:
SELECT PATINDEX( '%hoco%', 'chocolate' ); |
以下语句返回值 11:
SELECT PATINDEX( '%4_5_', '0a1A 2a3A 4a5A' ); |
以下语句返回 14,即字符串表达式中第一个非字母数字字符。如果数据库区分大小写,则可使用模式 '%[^a-z0-9]%'
代替 '%[^a-zA-Z0-9]%'
。
SELECT PATINDEX( '%[^a-zA-Z0-9]%', 'SQLAnywhere16 has many new features' ); |
以下语句可用于检索字符串中第一个非字母数字字符之前的所有内容(含第一个非字母数字字符):
SELECT LEFT( @string, PATINDEX( '%[^a-zA-Z0-9]%', @string ) ); |
下列语句创建表 myTable,并使用包含字母数字字符、空格(空白)和非字母数字字符的各种字符串填充该表。然后,SELECT 语句和后续结果显示如何使用 PATINDEX 在字符串中查找空格和非字母数字字符的起始位置:
CREATE TABLE myTable( col1 LONG VARCHAR ); INSERT INTO myTable (col1) VALUES( 'the quick brown fox jumped over the lazy dog' ), ( 'the quick brown fox $$$$ jumped over the lazy dog' ), ( 'the quick brown fox 0999 jumped over the lazy dog' ), ( 'the quick brown fox ** jumped over the lazy dog' ), ( 'thequickbrownfoxjumpedoverthelazydog' ), ( 'thequickbrownfoxjum999pedoverthelazydog' ), ( 'thequick$$$$brownfox' ), ( 'the quick brown fox$$ jumped over the lazy dog' ); SELECT col1, //position of first non-alphanumeric character or space: PATINDEX( '%[^a-z0-9]%', col1) AS blank_posn, //position of first non-alphanumeric char that isn't a space: PATINDEX( '%[^ a-z0-9]%', col1) AS non_alpha_char, //everything up to and including first non-alphanumeric char that isn't a space: LEFT ( col1, PATINDEX( '%[^ a-zA-Z0-9]%', col1) ) AS left_str, //first non-alphanumeric char that isn't a space, and everything to the right: SUBSTRING ( col1, PATINDEX( '%[^ a-zA-Z0-9]%', col1) ) AS sub_str FROM myTable; |
col1 | blank_posn | non_alpha_char | left_str | sub_str |
---|---|---|---|---|
the quick brown fox jumped over the lazy dog |
4 | 0 |
the quick brown fox jumped over the lazy dog |
|
the quick brown fox $$$$ jumped over the lazy dog |
4 | 21 |
the quick brown fox $ |
$$$$ jumped over the lazy dog |
the quick brown fox 0999 jumped over the lazy dog |
4 | 0 |
the quick brown fox 0999 jumped over the lazy dog |
|
the quick brown fox ** jumped over the lazy dog |
4 | 21 |
the quick brown fox * |
** jumped over the lazy dog |
thequickbrownfoxjumpedoverthelazydog |
0 | 0 |
thequickbrownfoxjumpedoverthelazydog |
|
thequickbrownfoxjum999pedoverthelazydog |
0 | 0 |
thequickbrownfoxjum999pedoverthelazydog |
|
thequick$$$$brownfox |
9 | 9 |
thequick$ |
$$$$brownfox |
the quick brown fox$$ jumped over the lazy dog |
4 | 20 |
the quick brown fox$ |
$$ jumped over the lazy dog |
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2013, SAP 股份公司或其关联公司. - SAP Sybase SQL Anywhere 16.0 |