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)
- If the trim_character is not specified, spaces will be removed by default.
- If BOTH is specified, the specified characters or spaces at the beginning and end will be removed.
- If specified as LEADING, remove the specified characters or spaces at the beginning.
- 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'