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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.