PostgreSQL ma funkcję POSITION() funkcja, która zwraca pierwszy indeks początkowy określonego podciągu w ciągu.
Jeśli podciąg nie istnieje w ciągu, zwracane jest zero.
Składnia
Składnia wygląda tak:
position ( substring text IN string text ) Przykłady
Oto przykład do zademonstrowania:
SELECT POSITION('and' IN 'Two Hands'); Wynik:
6
Jak wspomniano, jeśli podciąg nie zostanie znaleziony w ciągu, zwracane jest zero:
SELECT POSITION('squid' IN 'Two Hands'); Wynik:
0
Argumenty zerowe
Wartości null zwracają null :
\pset null '<null>'
SELECT
POSITION(null IN 'Two Hands') AS "1",
POSITION('and' IN null) AS "2"; Wynik:
1 | 2 --------+-------- <null> | <null>
Pominięcie argumentu
Pominięcie argumentu skutkuje błędem:
SELECT POSITION(); Wynik:
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.