Hive: Convert String to Integer

StringHiveUser Defined-FunctionsHiveql

String Problem Overview


I am looking for a Built-in UDF to convert values of a string column to integer in my hive table for sorting using SELECT and ORDER BY. I searched in the Language Manual, but no use. Any other suggestions also welcome.

String Solutions


Solution 1 - String

Solution 2 - String

If the value is between –2147483648 and 2147483647, cast(string_filed as int) will work. else cast(string_filed as bigint) will work

    hive> select cast('2147483647' as int);
    OK
    2147483647
    
    hive> select cast('2147483648' as int);
    OK
    NULL
    
    hive> select cast('2147483648' as bigint);
    OK
    2147483648

Solution 3 - String

It would return NULL but if taken as BIGINT would show the number

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrinivasView Question on Stackoverflow
Solution 1 - StringJoe KView Answer on Stackoverflow
Solution 2 - StringAnilView Answer on Stackoverflow
Solution 3 - StringraviView Answer on Stackoverflow