LTRIM()
SQL · String Functions
Theory
LTRIM(string) removes leading spaces, the ones at the start, and leaves the end of the string untouched. LTRIM(' Asha') returns 'Asha', but LTRIM('Asha ') is unchanged. In SQLite, Postgres and Oracle you can pass a second argument listing characters to remove: LTRIM('00042', '0') returns '42', a common way to strip leading zeros from codes.
In MySQL, LTRIM only removes spaces; to strip other characters use TRIM(LEADING '0' FROM code). SQL Server's LTRIM also accepts a character list in newer versions, so check yours. A gotcha with zero-stripping: LTRIM('000', '0') returns an empty string, not '0', because every character is a zero. If the code is purely numeric, CAST(code AS INTEGER) is often a cleaner way to drop leading zeros, but that changes the type to a number.
Why we use it
Leading spaces and zeros break matching: ' Pune' does not equal 'Pune', and '00042' does not equal '42'. LTRIM cleans the left side without touching meaningful characters at the end, such as trailing zeros in an amount.
When to use it
Use LTRIM to remove leading spaces or leading padding characters like zeros. Use TRIM to clean both sides at once and RTRIM for the end only. Watch for all-zero codes, which become empty strings.
How it works
- Take the string (and optional characters to strip)
- Look at the first character
- Is it in the strip set (space by default)? Yes: Remove it and look at the next one. No: Stop.
- The rest of the string is kept as is
- Return the left-trimmed string
Syntax
LTRIM(string)Example
SELECT LTRIM(' Asha');
-- 'Asha'Key points
- Only trims from the left side
- LTRIM(str, 'chars') works in SQLite, Postgres and Oracle
- MySQL LTRIM removes only spaces: use TRIM(LEADING 'x' FROM str)
- Stripping zeros from '000' leaves '', not '0'
- Pairs with RTRIM() for full control