PostgreSQLにはPOSITION()があります 文字列内の指定された部分文字列の最初の開始インデックスを返す関数。
文字列に部分文字列が存在しない場合は、ゼロが返されます。
構文
構文は次のようになります:
position ( substring text IN string text ) 例
デモンストレーションの例を次に示します。
SELECT POSITION('and' IN 'Two Hands'); 結果:
6
前述のように、文字列に部分文字列が見つからない場合は、ゼロが返されます:
SELECT POSITION('squid' IN 'Two Hands'); 結果:
0
ヌル引数
ヌル値はnullを返します :
\pset null '<null>'
SELECT
POSITION(null IN 'Two Hands') AS "1",
POSITION('and' IN null) AS "2"; 結果:
1 | 2 --------+-------- <null> | <null>
引数を省略する
引数を省略すると、エラーが発生します:
SELECT POSITION(); 結果:
ERROR: function pg_catalog.position() does not exist
LINE 1: SELECT POSITION();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.