BOTH is an optional keyword used inside the ANSI-standard TRIM function. It tells the database engine to eliminate the chosen character (or the default blank space) from both the left and right sides of the input string. When BOTH is omitted, many dialects assume BOTH by default, but writing it explicitly increases clarity and cross-dialect safety. BOTH cannot stand alone; it must appear inside a TRIM call and is always paired with FROM to separate the trim character(s) from the source string.Behavior:- If no trim character is supplied, TRIM(BOTH FROM str) removes leading and trailing spaces.- If a single character or string literal is supplied, TRIM(BOTH 'x' FROM str) removes every contiguous occurrence of that literal from both ends until a different character is encountered.- The inner content of the string is untouched.Caveats:- Some databases (notably older SQL Server versions) do not accept the ANSI syntax and instead expose TRIM(), LTRIM(), RTRIM().- Multibyte or Unicode variants trim correctly as long as the database collation treats them as single characters.- NULL input returns NULL.
TRIM, LEADING, TRAILING, LTRIM, RTRIM, REPLACE
SQL:1999 standard
BOTH removes characters on both sides, LEADING only at the beginning, and TRAILING only at the end of the string.
No. The ANSI syntax requires the keyword FROM to separate the trim characters from the source string.
A single space (ASCII 32) is assumed by default when no trim character is specified.
Use nested TRIM calls or REGEXP_REPLACE. TRIM only removes one literal or a space at a time.