Sub topic 6
Extracting Parts with `substring`, `left` and `right`
This chapter assumes basic SELECT syntax, PostgreSQL data types, and familiarity with NULL. The examples use PostgreSQL string functions, which operate on text values and use one-based character positions.
Choosing the right extraction function
Use substring when you know a starting position and possibly a length. Use left when you need characters from the beginning, and right when you need characters from the end.
| Function | Purpose | Example result |
|---|---|---|
| `substring` | Extract from a position | `substring('PostgreSQL' FROM 1 FOR 8)` → `PostgreS` |
| `left` | Keep characters from the beginning | `left('PostgreSQL', 4)` → `Post` |
| `right` | Keep characters from the end | `right('PostgreSQL', 3)` → `SQL` |
Using `substring`
The standard form is:
substring(value FROM start FOR count)start is a one-based character position. count is the maximum number of characters to return. You can omit FOR count when you want everything from the starting position to the end.
SELECT
substring('PostgreSQL' FROM 1 FOR 8) AS first_part,
substring('PostgreSQL' FROM 5) AS from_position_five,
substring('PostgreSQL', 5, 3) AS short_part;The first expression returns PostgreS. The second returns reSQL, and the third returns reS. PostgreSQL also supports the comma-separated form, substring(value, start, count).
A position beyond the end returns an empty string. A requested count larger than the remaining text returns only the remaining characters.
Using `left` and `right`
left(value, count) returns the first count characters. right(value, count) returns the last count characters.
SELECT
left('[email protected]', 8) AS prefix,
right('[email protected]', 3) AS suffix,
left('abc', 10) AS longer_than_value,
right('abc', 0) AS no_characters;The results are customer, com, abc, and an empty string. Asking for more characters than the value contains does not raise an error.
A common mistake is treating the second argument as a position. In left('PostgreSQL', 4), 4 means “return four characters,” not “stop at position four” in a general indexing operation.
Combining extraction with other expressions
These functions are useful for displaying a shortened value or separating a known-format identifier.
SELECT
email,
left(email, strpos(email, '@') - 1) AS username,
substring(email FROM strpos(email, '@') + 1) AS domain
FROM (VALUES
('[email protected]'),
('[email protected]')
) AS contacts(email);Here, strpos finds the position of @. The username ends immediately before that position, while the domain begins immediately after it.
Edge cases
The count argument can be zero, which returns an empty string. PostgreSQL also accepts negative counts for left and right. For example, left('abcdef', -2) removes the last two characters and returns abcd, while right('abcdef', -2) removes the first two and returns cdef. Use this behavior deliberately because a negative count can hide an arithmetic error in a calculated expression.
Exercise
What does substring('PostgreSQL' FROM 5 FOR 3) return, and why is it different from right('PostgreSQL', 3)?
Show answer
It returns reS because it starts at position 5 and takes three characters. right('PostgreSQL', 3) returns SQL because it takes the final three characters.