Function Reference

SUBSTR

Returns part of a string: length characters starting at position start.

PostgreSQL 17.5
SUBSTR(string, start[, length])

Parameters

stringtext
Source string
startinteger
Position of the first character, starting from 1
lengthintegeroptional
Number of characters, to the end of the string by default

Return value

A substring (text); an empty string if start is greater than the string length

Examples

From the fifth character and the first four

PostgreSQL 17.5
SELECT SUBSTR('PostgreSQL', 5) AS from_fifth,
	SUBSTR('PostgreSQL', 1, 4) AS first_four;
from_fifthfirst_four
greSQLPost

Initial of a name

PostgreSQL 17.5
SELECT member_name,
	SUBSTR(member_name, 1, 1) || '.' AS initial
FROM FamilyMembers;
member_nameinitial
Headley QuinceyH.
Flavia QuinceyF.
Andie QuinceyA.
Lela QuinceyL.
Annie QuinceyA.
Ernest ForrestE.
Constance ForrestC.
Wednesday AddamsW.

Details

It is the short form of SUBSTRING(string, start, length) with the same rules: positions before the string count towards length, and a negative length causes an error. Unlike MySQL, a negative start is not counted from the end: SUBSTR('PostgreSQL', -2, 5) returns Po. The last characters of a string are returned by RIGHT.

See also

Where to learn