PostgreSQL tem uma
POSITION() função que retorna o primeiro índice inicial de uma substring especificada dentro de uma string. Se a substring não existir na string, será retornado zero.
Sintaxe
A sintaxe fica assim:
position ( substring text IN string text ) Exemplos
Segue um exemplo para demonstrar:
SELECT POSITION('and' IN 'Two Hands'); Resultado:
6
Como mencionado, se a substring não for encontrada na string, zero será retornado:
SELECT POSITION('squid' IN 'Two Hands'); Resultado:
0
Argumentos nulos
Valores nulos retornam
null :\pset null '<null>'
SELECT
POSITION(null IN 'Two Hands') AS "1",
POSITION('and' IN null) AS "2"; Resultado:
1 | 2 --------+-------- <null> | <null>
Omitindo o argumento
Omitir o argumento resulta em um erro:
SELECT POSITION(); Resultado:
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.