Skip to content

TRIM

This function removes all trim_char characters specified in trim_pos from a string expr.

Syntax

sql
TRIM([[trim_pos] [trim_char] FROM] expr)

Arguments

expr: String to be processed.


trim_char (optional): A character to be removed from the left and right of expr. The default value is ' ' (a space). If the trim_char argument is omitted, all leading and trailing spaces are removed.

trim_char must be enclosed in single quotes (' ').


trim_pos (optional): Can take the following values:

  • LEADING: Removes all trim_char characters from the beginning of expr.
  • TRAILING: Removes all trim_char characters from the end of expr.
  • BOTH: Removes all trim_char characters from both the beginning and end of expr.

Usage Notes

  • If the trim_char argument is used, the query must include both the definition of trim_pos and the FROM clause.
  • If expr is a numeric value, the return value is also numeric.

Examples

sql
select 'trim(`   . &   `)' as function, '`' ||  trim('   . &   ') || '`'  as result
union all
select 'trim(LEADING ` ` from  `   . &   `)' as function, '`' || trim(LEADING ' ' from  '   . &   ') || '`' as result
union all 
select 'trim(LEADING `.` from  `.. &..`)' as function,'`' || trim(LEADING '.' from  '.. &..') || '`'  as result
union all 
select 'trim(BOTH `.` from  `.. &..`)' as function, '`' || trim(BOTH '.' from  '.. &..') || '`'  as result
union all 
select 'trim(BOTH `5` from  5600)' as function, '`' ||  trim(BOTH '5' from  5600) || '`'  as result
union all 
select 'trim(BOTH `0` from  5600)' as function, '`' || trim(BOTH '0' from  5600) || '`'  as result
       ;
+------------------------------------+--------+
| FUNCTION                           | RESULT |
+------------------------------------+--------+
| trim(`   . &   `)                  | `. &`  |
| trim(LEADING ` ` from `   . &   `) | `. & ` |
| trim(LEADING `.` from `.. &..`)    | ` &..` |
| trim(BOTH `.` from `.. &..`)       | ` &`   |
| trim(BOTH `5` from 5600)           | `600`  |
| trim(BOTH `0` from 5600)           | `56`   |
+------------------------------------+--------+