Hive TRIM Function: Usage & Syntax

In Hive, the TRIM function is used to remove leading and trailing spaces from a string. The syntax of the function is as follows:

TRIM([BOTH | LEADING | TRAILING] trim_character FROM input_string)
  1. If the trim_character is not specified, spaces will be removed by default.
  2. If BOTH is specified, the specified characters or spaces at the beginning and end will be removed.
  3. If specified as LEADING, remove the specified characters or spaces at the beginning.
  4. If TRAILING is specified, remove the specified characters or spaces at the end.

Here is an example:

SELECT TRIM('   hello   ') AS trimmed_string; -- 输出:'hello'

SELECT TRIM(BOTH 'x' FROM 'xhellox') AS trimmed_string; -- 输出:'hello'

SELECT TRIM(LEADING 'x' FROM 'xxhello') AS trimmed_string; -- 输出:'hello'

SELECT TRIM(TRAILING 'x' FROM 'helloxx') AS trimmed_string; -- 输出:'hello'
bannerAds