These functions help clean up text by removing extra spaces from the start, end, or both sides of a string.
TRIM, LTRIM, RTRIM in SQL
TRIM([LEADING | TRAILING | BOTH] ' ' FROM string) LTRIM(string) RTRIM(string)
TRIM removes spaces from both sides by default or can be told to remove only from the start (LEADING) or end (TRAILING).
LTRIM removes spaces only from the start (left side) of the string.
RTRIM removes spaces only from the end (right side) of the string.
SELECT TRIM(' hello ');
SELECT LTRIM(' hello ');
SELECT RTRIM(' hello ');
SELECT TRIM(LEADING ' ' FROM ' hello ');
This query shows how each function removes spaces differently from the string ' apple '.
SELECT TRIM(' apple ') AS trimmed_both, LTRIM(' apple ') AS trimmed_left, RTRIM(' apple ') AS trimmed_right;
Spaces mean the normal space character (ASCII 32). Some databases allow trimming other characters by specifying them.
TRIM is more flexible and standard SQL, while LTRIM and RTRIM are simpler shortcuts.
Always check your database documentation as syntax can slightly differ.
Use TRIM to remove spaces from both sides of a string.
Use LTRIM to remove spaces only from the start (left side).
Use RTRIM to remove spaces only from the end (right side).
Practice
LTRIM do to a string?Solution
Step 1: Understand LTRIM function purpose
LTRIM removes spaces only from the left side (start) of a string.Step 2: Compare with other trim functions
Unlike RTRIM (right side) or TRIM (both sides), LTRIM targets only the start.Final Answer:
Removes spaces only from the start (left side) of the string -> Option DQuick Check:
LTRIM = left side trim [OK]
- Confusing LTRIM with RTRIM
- Thinking LTRIM removes spaces from both sides
- Assuming LTRIM removes spaces inside the string
name in SQL?Solution
Step 1: Identify function to remove trailing spaces
RTRIM removes spaces from the right (end) side of a string.Step 2: Check syntax correctness
SELECT RTRIM(name) FROM table; is valid SQL syntax for this purpose.Final Answer:
SELECT RTRIM(name) FROM table; -> Option CQuick Check:
RTRIM trims right side spaces [OK]
- Using LTRIM instead of RTRIM for trailing spaces
- Using TRIM which removes both sides, not just trailing
- Using non-existent function REMOVE_TRAIL
SELECT TRIM(' Hello World '); be?Solution
Step 1: Understand TRIM function behavior
TRIM removes spaces from both the start and end of the string.Step 2: Apply TRIM to the given string
Spaces before and after 'Hello World' are removed, but spaces inside remain.Final Answer:
'Hello World' -> Option AQuick Check:
TRIM removes spaces both sides [OK]
- Expecting spaces inside the string to be removed
- Confusing TRIM with LTRIM or RTRIM
- Including quotes incorrectly in output
SELECT LTRIM(name) FROM users; but it does not remove spaces from the end of the names. What is the issue?Solution
Step 1: Understand LTRIM function limitation
LTRIM removes spaces only from the left (start) side of a string.Step 2: Identify why trailing spaces remain
Spaces at the end (right side) are not removed by LTRIM, so they remain.Final Answer:
LTRIM only removes spaces from the start, not the end -> Option BQuick Check:
LTRIM trims left side only [OK]
- Assuming LTRIM removes spaces from both sides
- Using quotes incorrectly around column names
- Confusing TRIM and LTRIM functions
products with a column code that sometimes contains spaces at both ends. You want to update the table to remove all leading and trailing spaces from code. Which SQL statement correctly does this?Solution
Step 1: Understand how to remove spaces from both ends
TRIM removes spaces from both the start and end of a string in one call.Step 2: Evaluate other options
LTRIM removes only leading spaces, RTRIM only trailing spaces. Neither alone removes both sides.Step 3: Check for invalid functions
REMOVE_SPACES is not a valid SQL function.Final Answer:
UPDATE products SET code = TRIM(code); -> Option AQuick Check:
TRIM removes both sides spaces [OK]
- Using LTRIM which removes only leading spaces
- Using RTRIM which removes only trailing spaces
- Using invalid function REMOVE_SPACES
Ready to prove it?
Pass the test and this pattern moves unknown
