The SQL TRIM() function removes the string head and end spaces
May 16, 2021
SQL
Table of contents
The SQL TRIM() function removes the string head and end spaces
The TRIM function in SQL is used to remove a header or end of a string of words. T
he most common use is to remove white space from the beginning or end of the word.
This function has different names in different libraries:
-
MySQL: TRIM( ), RTRIM( ), LTRIM( )
-
Oracle: RTRIM( ), LTRIM( )
-
SQL Server: RTRIM( ), LTRIM( )
The syntax of the various trim functions is as follows:
-
TRIM ("position" (position)
(string to be removed) FROM) : the possible values of "position" are LEADING (start), TRAILING (end), or BOTH (start and end). T
his function removes the string from the beginning, end, or start and end of the string. I
f we don't list what the string to remove is, the blank will be removed.
-
LTRIM : Removes
the blanks from the head of all strings.
-
RTRIM: Removes the
blanks at the end of all strings.
Example 1 TRIM ()
Results:
'Sample'
Example 2 LTRIM()
SELECT LTRIM(' Sample ');
Results:
'Sample '
Example 3 RTRIM()
SELECT RTRIM(' Sample ');
Results:
' Sample'