替换 hive-Nvl 和 COALESCE 中的空字符串已尝试



如何将空字符串(长度 0(替换为其他值? 已使用NvlCOALESCE但两者都不会替换为替换值,因为该值不为 null。我可以使用case语句,但如果有的话,请寻找内置函数。

由于您有空字符串,因此当我们使用合并或 nvl时,仅当我们在数据中有空值时才有效。这些函数不适用于空字符串。

使用空字符串:

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'='')

如果此属性未将空字符串显示为 null,那么我们需要使用case/if语句来替换空字符串。

您可以使用if statement

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  |
+-----------+--+

在 Hive 中,空字符串被视为通常的可比较值,而不是 NULL。这就是为什么没有内置函数的原因。

使用案例语句:

case when col='' or col is null then 'something' else col end

最新更新