Jump to: navigation, search

PostgreSQL SUBSTRING

From w3cyberlearnings

Contents

Postgresql substring

This function extracts substring from a string. It can be used the POSIX regular expression matching. Please check the example.

Syntax substring

  • string is the string
  • start is the location to start extract
  • length is the length of the string to extract.
substring(string start length);


OR

substring(string from pattern for escape)

Example 1

test2=# SELECT substring('lovely' from 5 for 2);
 substring 
-----------
 ly
(1 row)

Example 2:regular expression

test2=# SELECT substring('great' from '...$');
 substring 
-----------
 eat
(1 row)

test2=# SELECT substring('great' from '^..');
 substring 
-----------
 gr
(1 row)


Example 3

test2=# SELECT user_firstname,
test2-# substring(user_firstname from 1 for 1)
test2-# FROM profile;
 user_firstname | substring 
----------------+-----------
 Jim            | J
 king           | k
 Jammi          | J
 Bob            | B
                | 
(5 rows)

Related Links


Navigation
Web
SQL
MISC
References