PostgreSQL - POSITION Function
Last Updated :
18 Jul, 2024
The PostgreSQL POSITION()
function is used to find the location of a substring within a string. This function is particularly useful for text manipulation and searching operations within your database.
Let us better understand the POSITION Function in PostgreSQL from this article.
Syntax
POSITION(substring IN string)
Parameters:
Let's analyze the above syntax:
- substring: The substring that you want to locate within the string.
- string: The string in which you are searching for the substring.
Return Value:
The POSITION() function returns the location of the searched substring in integer form that represents the location of the substring within the string. It returns zero (0) if no matching substring found.
PostgreSQL POSITION Function Examples
Let us take a look at some of the examples of POSITION Function in PostgreSQL to better understand the concept.
Example 1: Finding the Position of a Substring
The following statement returns the position of the 'Tutorial' in the string 'GeeksForGeeks Tutorial':
SELECT POSITION('Tutorial' IN 'GeeksForGeeks Tutorial');
Output:

Explanation: This indicates that the substring 'Tutorial' starts at the 13th position of the string 'GeeksForGeeks Tutorial'.
Example 2: Finding the First Instance of a Substring
The POSITION() function returns the location of the first instance of the substring in the strings shown in the below example:
SELECT POSITION('am' IN 'I am a geek');
Output:

Explanation: Here, the substring 'am' starts at the 3rd position of the string 'I am a geek'.
Important Points About PostgreSQL POSITION Function
- The
POSITION()
function is case-sensitive. If the case of the substring and the string do not match, the function will return 0. - Unlike some programming languages that use zero-based indexing, PostgreSQL
POSITION()
returns a one-based index, meaning the first character of the string is at position 1. POSITION
()
only returns the position of the first occurrence of the substring.POSITION()
can be effectively combined with other PostgreSQL string functions like SUBSTRING
()
, LENGTH()
, and TRIM()
for more complex string manipulations.
Similar Reads
PostgreSQL - NOW() Function The NOW() function in PostgreSQL is a powerful and essential tool for retrieving the current date and time. This is particularly useful when recording actions like adding or updating records in our database. Using PostgreSQL NOW() function, we can efficiently track when events occur, making the data
4 min read
PostgreSQL- LPAD Function The LPAD() function in PostgreSQL is a powerful tool for padding a string to the left, ensuring it reaches a specified length by filling it with a designated character or characters. This function can be particularly useful in data formatting and report generation.Let us better understand the LPAD F
2 min read
PostgreSQL - LAG Function In PostgreSQL, the LAG() function is a powerful window function that allows you to access data from a previous row within the same result set. Itâs particularly useful for comparing values in the current row with values in the preceding row, making it ideal for analytical queries in PostgreSQL.For e
5 min read
PostgreSQL - LEAD Function In PostgreSQL, the LEAD() function is a powerful window function used to access a row that follows the current row at a specific physical offset. This function is generally employed to compare the value of the current row with the value of the next row following the current row.Let us better underst
3 min read
PostgreSQL - Function Parameters In PostgreSQL, functions provide an efficient way to encapsulate logic, perform calculations, and handle complex tasks within a database. A thorough understanding of PostgreSQL function parameters is essential for writing flexible and optimized functions.In this article, we will analyze different ty
5 min read