Function or operator · Text, numbers and dates

TRIM

TRIM removes the spaces at both ends of a string. It does not remove spaces in the middle, and using it in SELECT does not change the stored value.

TRIM example

The literal has three spaces before and after the text. TRIM returns only the meaningful content:

SQL
SELECT TRIM('   SQL for everyone   ') AS clean_text;

What to look for: The result is “SQL for todos”, with no leading or trailing spaces. TRIM does not remove the spaces between words, and it does not update a table column on its own.

Check the result with small data

The names have spaces at the start and end.

Self-contained query · SQLite
WITH users(name) AS (
  VALUES ('  Amy  '),(' Liam ')
)
SELECT TRIM(name) AS clean FROM users
ORDER BY clean;
Query result
clean
Amy
Liam

TRIM removes the outer spaces but keeps the ones inside multi-part names. It does not fix the original data without an explicit UPDATE.

Check your understanding: Which function would you use to remove only the leading spaces?

Show answer

LTRIM(name). RTRIM removes only the trailing spaces.

How TRIM is used

LTRIM trims the left side and RTRIM the right side. How you specify characters other than spaces varies between engines, so check the documentation before using an advanced variant.

Common mix-ups with TRIM

Removing every space with REPLACE

Unwanted result
SELECT REPLACE('  Amy Wilson  ', ' ', '');

That approach returns AnaTorres: it also deletes the legitimate space between first and last name. If the only extra spaces are at the ends, use TRIM.

Key differences between engines

SQLite accepts a second argument listing the characters to remove. PostgreSQL offers the standard SQL form with LEADING, TRAILING or BOTH. MySQL supports several forms of TRIM. For ordinary spaces, TRIM(text_value) is the most portable option.