Strings

LEFT

Takes the given number of characters from the start of a string.

Syntax

LEFT(<text>, <count>)

A shorter way to write the most common SUBSTRING: LEFT(name, 1) is the first letter. A negative count means “everything except”: LEFT(<text>, -4) drops the last four characters. Its mirror function is RIGHT, which takes characters from the end.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The first characters
SELECT LEFT('PostgreSQL', 4) AS first_four;
first_four
Post
A short department code
SELECT DISTINCT department,
LEFT(department, 3) AS short_code
FROM employees
ORDER BY department;
departmentshort_code
backendbac
designdes
frontendfro
Everything except the end
SELECT LEFT('report.csv', -4) AS without_extension;
without_extension
report

Watch out

A negative count isn't an error — it means “everything except”. That hides a subtle bug: for an email without @, LEFT(email, POSITION('@' IN email) - 1) becomes LEFT(email, -1) — everything except the last character.

A count larger than the length isn't an error either: LEFT('SQL', 10) simply returns SQL.

Related functions