LEFT
Returns the first n characters of a string.
PostgreSQL 17.9
LEFT(string, n)- string
- Source stringtext
- n
- Number of characters; a negative n keeps all but the last |n|integer
Return value
The first n characters; with a negative n, all characters except the last |n|
Examples
First three characters
PostgreSQL 17.9
SELECT LEFT('sql-academy', 3);All but the last eight characters
PostgreSQL 17.9
SELECT LEFT('sql-academy', -8);Name before the first space
PostgreSQL 17.9
SELECT member_name,
LEFT(member_name, STRPOS(member_name, ' ') - 1) AS first_name
FROM FamilyMembers;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.