HiveQL - How to find the column value is numeric or not using any UDF?

后端 未结 2 1918
清酒与你
清酒与你 2020-12-31 17:28

Basically i would like to return rows based on one column value.

If the column contains non numeric values, then return those

2条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 18:19

    Use cast(expr as ). A null is returned if the conversion does not succeed.

    case when cast(col as double) is null then 'N' else 'Y' end as isNumber 
    

    or simply use Boolean expression in the WHERE: cast(col as double) is not null

    Also you can create isNumber macro:

    create temporary macro isNumber(s string)
           cast(s as double) is not null;
    

    And use it in your queries:

    hive> select isNumber('100.100'), isNumber('100'), isNumber('.0'), isNumber('abc');
    OK
    _c0     _c1     _c2     _c3
    true    true    true    false
    

    If you need to check for Integer then use cast(s as Int)

    This approach works correctly with negative and fractional numbers.

提交回复
热议问题