在 Hive 中,去除字符串两端的空格(前后空格)可以使用 `trim` 函数。这个函数可以去除字符串开头和结尾的空白字符。
`TRIM` 函数:TRIM(string) `string`: 要去除空格的字符串。
示例:
假设有一个表 `users1`,其中有一个字段 `username`,有些记录的 `username` 两端有空格。你可以使用 `TRIM` 函数去掉这些空格。
其中,第一条记录左侧有空格,第二条记录右侧有空格,第三条记录两侧都有空格,第四条记录无空格。
(1)返回去除前后空格后的 `username` 字段值(TRIM)。
select trim(username) as username from user1 where userid=3;
(2)去除左侧空格(LTRIM)。
select ltrim(username) username from user1 where userid=1;
(3)去除右侧空格(RTRIM)。
select rtrim(username) username from user1 where userid=2;
这样你可以根据需要去除字符串中的空格。