Lesson

MySQL LTRIM() Function

10 min reading
Free Course

MySQL LTRIM() Function

The LTRIM() function removes all leading ASCII space characters (' ') from the start of a string. Trailing spaces and interior spaces inside the text stay untouched.

Syntax

LTRIM(str)
  • str: The input string or column to clean.
  • Return value: String without leading spaces, or NULL if str is NULL.

LTRIM() works on both text columns and string literals, making it handy when cleaning messy CSV imports.

Practical Example

Imported customer records in demo_customers often arrive with stray spaces before first_name or city. Here is how LTRIM() cleans only the left edge while leaving trailing spaces intact.

SELECT
    customer_id,
    CONCAT('[', city, ']') AS raw_city,
    CONCAT('[', LTRIM(city), ']') AS left_trimmed,
    CHAR_LENGTH(LTRIM(city)) AS trimmed_chars
FROM demo_customers
ORDER BY customer_id;
customer_id raw_city left_trimmed trimmed_chars
1 [ Austin] [Austin] 6
2 [ Denver ] [Denver ] 8
3 [ ] [] 0
4 NULL NULL NULL

Row 2 shows that LTRIM() strips the two leading spaces before 'Denver' while keeping the two trailing spaces (8 characters total).

Common Mistakes & NULL Behavior

  • Tabs and newlines are NOT removed: LTRIM() strips only regular spaces (' ', ASCII 32). Leading tabs ( ), carriage returns ( ), and newlines ( ) remain in the string.
  • Trying to pass a second character argument: Unlike SQL Server or PostgreSQL, MySQL's LTRIM(str) accepts only one argument. Passing LTRIM(sku, '0') triggers a syntax error; use TRIM(LEADING '0' FROM sku) instead.
  • NULL input returns NULL: Missing values stay NULL, while a string of only spaces becomes ''.

Frequently Asked Questions

How do I strip both leading and trailing spaces at once?

Use TRIM(str). It removes spaces from both sides without nesting LTRIM(RTRIM(str)).

How do I remove leading zeros instead of spaces?

Use TRIM(LEADING '0' FROM str) rather than LTRIM(), because LTRIM() cannot target custom characters in MySQL.

Does LTRIM() affect spaces between words?

No. Spaces inside 'New York' stay intact; only spaces before 'N' are removed.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum