如何将空字符串(长度为0)替换为其他值?已经使用了Nvl
和COALESCE
,但是两者都不能用于替换非空值。我可以使用case
语句,但是希望能够找到内置函数。
由于您使用了空字符串,因此当我们使用coalesce或nvl时,只有在数据中存在null值时才能起作用。这些函数无法处理空字符串。
对于空字符串:
hive> select coalesce(string(""),"1");
+------+--+
| _c0 |
+------+--+
| |
+------+--+
hive> select nvl(string(""),"1");
+------+--+
| _c0 |
+------+--+
| |
+------+--+
带有空值的情况:
hive> select coalesce(string(null),"1");
+------+--+
| _c0 |
+------+--+
| 1 |
+------+--+
hive> select nvl(string(null),"1");
+------+--+
| _c0 |
+------+--+
| 1 |
+------+--+
TBLPROPERTIES('serialization.null.format'='')
case/if
语句来替换空字符串。if语句
if(boolean testCondition, T valueTrue, T valueFalseOrNull)
hive> select if(length(trim(<col_name>))=0,'<replacement_val>',<col_name>) from <db>.<tb>;
例子:
hive> select if(length(trim(string("")))=0,'1',string("col_name"));
+------+--+
| _c0 |
+------+--+
| 1 |
+------+--+
hive> select if(length(trim(string("1")))=0,'1',string("col_name"));
+-----------+--+
| _c0 |
+-----------+--+
| col_name |
+-----------+--+
case when col='' or col is null then 'something' else col end