PostgreSQL strpos() Function

The PostgreSQL strpos() function returns the first starting index of a specified substring within a string.

The strpos() function is the same as a position() function, but the order of their arguments is different.

strpos() Syntax

This is the syntax of the PostgreSQL strpos() function:

strpos(string, substring)

strpos(string, substring) is equivalent to position(substring IN string).

Parameters

string

Required. The string.

substring

Required. A substring to search for.

Return value

The PostgreSQL strpos() function returns the starting index of the first occurrence of the substring substring in the string string. This function returns zero if the specified substring does not appear in the string.

If any parameter is NULL, the function will return NULL.

strpos() Examples

This example shows how to use the strpos() function to find the index of a substring in a string.

SELECT strpos('hello world', 'world');
 strpos
--------
      7