The input string goes through one of the trim functions (TRIM, LTRIM, RTRIM) which remove spaces or specified characters from either both ends, left end, or right end, then returns the cleaned string.
Execution Sample
PostgreSQL
SELECT TRIM(' hello ') AS trimmed_full,
LTRIM(' hello ') AS trimmed_left,
RTRIM(' hello ') AS trimmed_right;
This query trims spaces from both ends, left end, and right end of the string ' hello ' using TRIM, LTRIM, and RTRIM respectively.
Execution Table
Step
Function
Input String
Operation
Output String
1
TRIM
' hello '
Remove spaces from both ends
'hello'
2
LTRIM
' hello '
Remove spaces from left end
'hello '
3
RTRIM
' hello '
Remove spaces from right end
' hello'
4
TRIM(BOTH 'x' FROM 'xxhelloxx')
'xxhelloxx'
Remove 'x' from both ends
'hello'
5
LTRIM('xxhelloxx', 'x')
'xxhelloxx'
Remove 'x' from left end
'helloxx'
6
RTRIM('xxhelloxx', 'x')
'xxhelloxx'
Remove 'x' from right end
'xxhello'
7
TRIM(BOTH FROM ' hello ')
' hello '
Default trim spaces both ends
'hello'
8
LTRIM(' hello ')
' hello '
Default trim spaces left end
'hello '
9
RTRIM(' hello ')
' hello '
Default trim spaces right end
' hello'
10
End
All trimming operations complete
N/A
💡 All trimming operations finished, strings returned with specified characters removed from ends.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
After Step 4
After Step 5
After Step 6
Input String
' hello '
'hello'
'hello '
' hello'
'xxhelloxx'
'xxhelloxx'
'xxhelloxx'
Output String
N/A
'hello'
'hello '
' hello'
'hello'
'helloxx'
'xxhello'
Key Moments - 3 Insights
Why does TRIM remove spaces from both ends but LTRIM and RTRIM only remove from one side?
TRIM is designed to remove characters from both the left and right ends simultaneously, as shown in execution_table rows 1 and 4. LTRIM only removes from the left (rows 2 and 5), and RTRIM only from the right (rows 3 and 6). This is why their outputs differ.
What happens if I specify a character other than space in TRIM, LTRIM, or RTRIM?
When you specify a character, the functions remove that character from the specified ends instead of spaces. For example, in rows 4, 5, and 6, 'x' is removed from the ends instead of spaces.
If I don't specify a character, what does TRIM remove by default?
By default, TRIM, LTRIM, and RTRIM remove spaces if no character is specified, as shown in rows 7, 8, and 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 2. What is the output string after LTRIM(' hello ')?
A'hello'
B' hello'
C'hello '
D' hello '
💡 Hint
Check the Output String column in row 2 of the execution_table.
At which step does TRIM remove the character 'x' from both ends?
AStep 4
BStep 5
CStep 1
DStep 6
💡 Hint
Look for TRIM with 'x' character removal in the Function column.
If you use RTRIM('xxhelloxx', 'x'), what will be the output string?
A'hello'
B'xxhello'
C'helloxx'
D'xxhelloxx'
💡 Hint
Refer to execution_table row 6 Output String.
Concept Snapshot
TRIM removes characters from both ends of a string.
LTRIM removes characters from the left end only.
RTRIM removes characters from the right end only.
By default, they remove spaces unless a specific character is given.
Syntax examples:
TRIM(BOTH 'x' FROM string), LTRIM(string, 'x'), RTRIM(string, 'x')
Full Transcript
This visual execution shows how PostgreSQL's TRIM, LTRIM, and RTRIM functions work. The input string is processed by one of these functions to remove spaces or specified characters from either both ends, the left end, or the right end. The execution table traces each step with input, operation, and output. Key moments clarify common confusions about default behavior and character specification. The visual quiz tests understanding of outputs at specific steps. The snapshot summarizes syntax and behavior for quick reference.