SQL TRIM Function
Quick summary: The SQL TRIM() function removes leading and trailing spaces from a string.
SQL TRIM Syntax
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)
SQL
SQL TRIM Basic examples
SELECT TRIM(' hello ');
SQL
Output:
hello
Removes whitespace from both sides.
SQL TRIM Real-world usage
SELECT * FROM users WHERE TRIM(email) = 'test@example.com';
SQL
Output:
Matching users
Normalizes user input.
SQL TRIM Edge cases
SELECT TRIM(NULL);
SQL
Output:
NULL
Returns NULL when input is NULL.
SQL TRIM Common mistakes
Assuming TRIM removes inner spaces
TRIM only removes leading and trailing spaces.
Incorrect
TRIM('a b')
Correct
REPLACE('a b',' ','')
Use REPLACE for inner characters.
SQL TRIM Frequently Asked Questions
What does TRIM() do in SQL?
Removes leading and trailing spaces or characters.
Use case of TRIM()?
Cleaning input data.
Syntax?
TRIM([characters FROM] string).
Common mistake?
Expecting it to remove inner spaces.
Return type?
String.
Handles NULL?
Returns NULL.
Performance?
Fast.
Alternative?
LTRIM, RTRIM.
Used in WHERE?
Yes.
Supports custom characters?
Yes.
Used in cleaning pipelines?
Yes.
Best practice?
Trim before storing data.