在Hive中,TRIM函數(shù)用于刪除字符串的開頭和結(jié)尾的空格。該函數(shù)的語法如下:
```sql
TRIM([BOTH | LEADING | TRAILING] trim_character FROM input_string)
```
- 如果不指定trim_character,則默認(rèn)刪除空格。
- 如果指定BOTH,則刪除開頭和結(jié)尾的指定字符或空格。
- 如果指定LEADING,則刪除開頭的指定字符或空格。
- 如果指定TRAILING,則刪除結(jié)尾的指定字符或空格。
示例如下:
```sql
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'
```