PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
Home / PostgreSQL String Functions / PostgreSQL POSITION

PostgreSQL POSITION

The PostgreSQL POSITION() function returns the location of a substring in a string.

Syntax

The following illustrates the syntax of the PostgreSQL POSITION() function:

1
POSITION(substring in string)

Arguments

The POSITION() function requires two arguments:

1) substring

The substring argument is the string that you want to locate.

2) string

The string argument is the string for which the substring is searched.

Return Value

The POSITION() function returns an integer that represents the location of the substring within the string.

The POSITION() function returns zero (0) if the substring is not found in the string. It returns null if either substring or string argument is null.

Examples

The following example returns the position of the 'Tutorial' in the string 'PostgreSQL Tutorial':

1
SELECT POSITION('Tutorial' IN 'PostgreSQL Tutorial');

The result is as follows:

1
2
3
4
position
----------
       12
(1 row)

Note that the POSITION() function searches for the substring case-insensitively.

See the following example:

1
SELECT POSITION('tutorial' IN 'PostgreSQL Tutorial');

It returns zero (0), indicating that the string tutorial does not exist in the string 'PostgreSQL Tutorial'.

Remarks

The POSITION() function returns the location of the first instance of the substring in the string.

Consider the following example:

1
SELECT POSITION('is' IN 'This is a cat');

The result is:

1
2
3
4
position
----------
        3
(1 row)

Even though the substring 'is' appears twice in the string 'This is a cat', the POSITION() function just returned the first match.

In this tutorial, you have learned how to use the PostgreSQL POSITION() function to locate a substring in a string.

Previous Tutorial: PostgreSQL Letter Case Functions
Next Tutorial: PostgreSQL Substring

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL CURRENT_TIMESTAMP
  • PostgreSQL CURRENT_TIME
  • PostgreSQL CURRENT_DATE
  • PostgreSQL Date Functions
  • PostgreSQL EXTRACT
  • PostgreSQL String Functions
  • PostgreSQL SPLIT_PART
  • PostgreSQL CHR
  • PostgreSQL ASCII
  • PostgreSQL Aggregate Functions

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.