Function Reference

LEFT

Returns the first n characters of a string.

PostgreSQL 17.5
LEFT(string, n)

Parameters

stringtext
Source string
ninteger
Number of characters, can be negative

Return value

The first n characters; with a negative n, all characters except the last |n|

Examples

First three characters

PostgreSQL 17.5
SELECT LEFT('sql-academy', 3);
left
sql

All but the last eight characters

PostgreSQL 17.5
SELECT LEFT('sql-academy', -8);
left
sql

Name before the first space

PostgreSQL 17.5
SELECT member_name,
	LEFT(member_name, STRPOS(member_name, ' ') - 1) AS first_name
FROM FamilyMembers;
member_namefirst_name
Headley QuinceyHeadley
Flavia QuinceyFlavia
Andie QuinceyAndie
Lela QuinceyLela
Annie QuinceyAnnie
Ernest ForrestErnest
Constance ForrestConstance
Wednesday AddamsWednesday

Details

If n is greater than the string length, the whole string is returned. In MySQL a negative n gives an empty string, while here it cuts characters off the end.

See also

Where to learn