PostgreSQL – STRPOS Function
The strpos
function in PostgreSQL database is used to find the position of a substring within a given string. It returns the position (counting from 1) of the first occurrence of the substring within the string. If the substring is not found, it returns 0.
Here’s the syntax for the strpos
function:
strpos(source_string, search_string)
source_string
: This is the string in which you want to search for thesearch_string
.search_string
: This is the substring you want to find within thesource_string
.
Here’s an example of how to use the strpos
function:
SELECT strpos('Hello, world!', 'world');
In this example, the strpos
function is used to find the position of the substring 'world'
within the string 'Hello, world!'
. The result of this query will be 7
because the substring 'world'
starts at the 7th position in the source_string
.
If the substring is not found, strpos
returns 0
. For example:
SELECT strpos('Hello, world!', 'foo');
In this case, the result will be 0
because the substring 'foo'
is not present in the source_string
.
You can use the strpos
function to locate the position of a substring within a larger string, and it can be useful for various text manipulation tasks in SQL queries.