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);All but the last eight characters
PostgreSQL 17.5
SELECT LEFT('sql-academy', -8);Name before the first space
PostgreSQL 17.5
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.