// strings reference

SQL LTRIM function

Removes the longest run of the given characters (spaces by default) from the start of a string.

类别
Strings
Returns
string
In this category
134 of 172

语法

LTRIM(string [, characters])

引擎支持

引擎Support备注
SQL Server 是 The characters argument needs SQL Server 2022; before that, spaces only.
PostgreSQL 是 Supported under this name.
MySQL 是 One argument only; trims spaces. TRIM(LEADING x FROM s) strips a repeated string.
SQLite 是 Supported under this name.
Oracle 是 Supported under this name.

示例

Any engine

SELECT LTRIM('   padded') AS trimmed;  -- padded

PostgreSQL

SELECT LTRIM('00123', '0') AS trimmed;  -- 123

MySQL

SELECT TRIM(LEADING '0' FROM '00123') AS trimmed;  -- 123

值得了解

  • Every engine has LTRIM(string). The character-set argument exists in PostgreSQL, Oracle, SQLite and SQL Server 2022+; MySQL's LTRIM takes one argument and removes spaces only.
  • The argument is a set of characters, not a prefix: LTRIM('ababc', 'ab') is 'c' because every leading a or b is peeled away.
  • Trimming stops at the first character outside the set; the interior of the string is never touched.

Questions about LTRIM

Does LTRIM remove a prefix string?

No, it removes any leading characters from the set. To strip an exact prefix, test it with LIKE 'prefix%' and take SUBSTR from the prefix length + 1. MySQL's TRIM(LEADING 'prefix' FROM s) also works but removes repeated copies.

Why are tabs still present after LTRIM in SQL Server?

Before SQL Server 2022, LTRIM trims spaces only. From 2022 pass the set: LTRIM(s, ' ' + CHAR(9) + CHAR(10) + CHAR(13)). Elsewhere pass the set as the second argument.

Is LTRIM case-sensitive?

PostgreSQL, Oracle and SQLite match characters exactly. SQL Server 2022 compares with the collation, so under a case-insensitive collation LTRIM('aab', 'A') returns 'b'.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.