TRIM is a standard SQL string function that cleans up text by deleting unwanted characters from the start, the end, or both ends of a string. By default it targets space characters, but you can supply any single-byte or multibyte literal or expression. The optional LEADING, TRAILING, or BOTH keyword specifies which side to operate on; if omitted, BOTH is assumed. TRIM never alters characters inside the middle of the string and returns a value of the same data type as the input (CHAR, VARCHAR, TEXT, etc.). Most databases also offer shorthand forms such as TRIM(string) or dialect-specific variants like LTRIM and RTRIM.Important caveats:- Case sensitivity, collation, and Unicode normalization can affect what qualifies as a match.- When trimming multibyte characters, make sure the literal matches the exact byte sequence.- Some dialects (e.g., MySQL) allow only one character in the specification, while others (e.g., PostgreSQL) accept a set of characters.- Attempting to TRIM NULL returns NULL.
BOTH | LEADING | TRAILING
(keyword) - Side of the string to trim. Default BOTH.characters
(string) - The character(s) to remove. Default is space.string
(string) - The source expression to be trimmed.LTRIM, RTRIM, SUBSTRING, REPLACE, REGEXP_REPLACE, CONCAT
SQL:1999
TRIM can remove any literal or expression that evaluates to one or more characters. If omitted, it trims ordinary spaces.
Pass the specific character in a literal, e.g., TRIM('\n' FROM column) or TRIM('\t' FROM column). Ensure escape sequences match your dialect.
No. If the input expression is NULL, TRIM returns NULL.
LTRIM removes characters only from the left side, RTRIM from the right side, while TRIM can handle either side or both.