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

  1. Take the string (and optional characters to strip)
  2. Look at the first character
  3. Is it in the strip set (space by default)? Yes: Remove it and look at the next one. No: Stop.
  4. The rest of the string is kept as is
  5. Return the left-trimmed string

Syntax

LTRIM(string)

Example

SELECT LTRIM('   Asha');
-- 'Asha'

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic