Skip to content

SUBSTRING

Return an exact set of characters from a string. See also SUBSTR and SUBSTRING (Pattern Match).

SUBSTRING(string [FROM start] [FOR count])

Note: SUBSTRING and SUBSTR are not synonymous. SUBSTRING returns a VARCHAR data type, but SUBSTR returns the data type of its input string.

string
An expression that evaluates to a character string.
FROM start
Starting position for the substring (an integer).
FOR count
Number of characters in the substring.

For example, select the first 10 characters from each team name:

premdb=# SELECT name, SUBSTRING(name FROM 1 FOR 10) FROM team;
         name           | substring  
-------------------------+------------
 Arsenal                 | Arsenal
 Aston Villa             | Aston Vill
 Barnsley                | Barnsley
 Birmingham City         | Birmingham
 Blackburn Rovers        | Blackburn 
 Blackpool               | Blackpool
...

For example, select the last four characters from the season_name column:

premdb=# SELECT SUBSTRING(season_name FROM 6 FOR 4) FROM season;
 season_name | substring 
-------------+-----------
 1992-1993   | 1993
 1993-1994   | 1994
 1994-1995   | 1995
 1995-1996   | 1996
 1996-1997   | 1997
 1997-1998   | 1998
 1998-1999   | 1999
 1999-2000   | 2000
 2000-2001   | 2001
...