// strings reference

SQL BTRIM function

Removes every leading and trailing character that appears in a character set. Spaces by default; name the set to strip quotes, slashes, zeros.

Catégorie
Strings
Returns
string
In this category
33 of 172

Syntaxe

BTRIM(string [, characters])

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Not built in No built-in equivalent.
PostgreSQL Oui ltrim/rtrim are the one-sided siblings
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Not built in No built-in equivalent.

Exemples

PostgreSQL

SELECT BTRIM('xyxabcxy', 'xy') AS core;  -- abc

PostgreSQL

SELECT BTRIM('  padded  ') AS plain;  -- padded

PostgreSQL

SELECT BTRIM('"quoted"', '"') AS unquoted;  -- quoted

Bon à savoir

  • It strips any combination of the given characters, in any order and count: BTRIM('xyxabcxy', 'xy') is 'abc'; it is not a prefix/suffix match.
  • Without the second argument it strips spaces only, never tabs or newlines.
  • Set trims elsewhere: trim(s, 'xy') in SQLite (which has no TRIM ... FROM syntax) and TRIM('xy' FROM s) in SQL Server 2017+. MySQL's TRIM(BOTH 'xy' FROM s) removes the substring 'xy', not the characters, so 'xyxabcxy' becomes 'xabc'. Oracle's TRIM takes a single character only; LTRIM(RTRIM(s, 'xy'), 'xy') handles a set.
  • Interior characters are untouched; only the ends are cleaned.

Questions about BTRIM

Is BTRIM the same as TRIM?

Yes in PostgreSQL: BTRIM(s, chars) equals TRIM(BOTH chars FROM s). The other engines spell it TRIM; BTRIM is the PostgreSQL name.

Why did BTRIM eat more characters than I wanted?

It removes characters from the set at both ends until it meets one outside the set. BTRIM('000120', '0') returns '12' because the trailing zero goes too; use LTRIM(s, '0') to strip leading zeros only.

Does BTRIM remove tabs and newlines?

Not unless you list them: BTRIM(s, E' \t\n'). The default set is just the space character.

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: PostgreSQL.

// 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.