What is the replacement of NULLIF in Hive?

可紊 提交于 2019-12-04 09:51:44

Just use case:

select (case when A is null or A = '' then . . . end)

This is standard SQL, so it will work in Hive and elsewhere.

For your particular problem:

select (case when A is not null and A <> '' then A
             when B is not null and B <> '' then B
             else C
        end)

You can actually shorten this to:

select (case when A <> '' then A
             when B <> '' then B
             else C
        end)

because NULL values fail comparisons. I would use this version but often people learning SQL prefer the more explicit version with the not null comparison.

Just use case syntax like following below :

select
     coalesce(
              case when A = '' then NULL else A end
              ,case when B = '' then NULL else B end
              ,case when C = '' then NULL else C end
             ) as D
 from myTable

Hope can solved your problem. Thank you..

Another HiveQL specific option is here:

create temporary macro nullify(s string) if(s = '', null, s);
--
select coalesce(nullify(A), nullify(B), nullify(C)) as D ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!