文字列のパターンが最初に出現した開始位置を表す整数を返します。
PATINDEX( '%pattern%', string-expression )
pattern 検索するパターン。先頭の % ワイルドカードを省略すると、PATINDEX 関数は、パターンが文字列の先頭に出現する場合は 1 を返し、それ以外の場合は 0 を返します。
パターンは、LIKE 比較と同じワイルドカードを使用します。次のワイルドカードがあります。
ワイルドカード | 一致するもの |
---|---|
_ (アンダースコア) | 任意の 1 文字 |
% (パーセント記号) | 0 個以上の文字からなる任意の文字列 |
[] | 指定範囲内、または一連の指定文字の任意の 1 文字 |
[^] | 指定範囲外、または一連の指定文字以外の任意の 1 文字 |
string-expression パターンを検索する文字列。
INT
PATINDEX 関数は、パターンが最初に出現した開始位置を返します。パターンが見つからない場合は、0 を返します。
SQL/2008 ベンダー拡張。
次の文は、値 2 を返します。
SELECT PATINDEX( '%hoco%', 'chocolate' ); |
次の文は、値 11 を返します。
SELECT PATINDEX( '%4_5_', '0a1A 2a3A 4a5A' ); |
次の文は、14 を返します。これは、文字列式の最初の英数字以外の文字です。データベースで大文字と小文字が区別されない場合は、'%[^a-zA-Z0-9]%'
の代わりにパターン '%[^a-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 で意見交換できます
|
Copyright © 2013, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |