Data type conversions
Similar to Java, Hive supports both implicit type conversion and explicit type conversion.
Primitive type conversion from a narrow to a wider type is known as implicit conversion. However, the reverse conversion is not allowed. All the integral numeric types, FLOAT
, and STRING
can be implicitly converted to DOUBLE
, and TINYINT
, SMALLINT
, and INT
can all be converted to FLOAT
. BOOLEAN
types cannot be converted to any other type. In the Apache Hive wiki, there is a data type cross table describing the allowed implicit conversion between every two types in Hive and this can be found at https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types.
Explicit type conversion is using the CAST
function with the CAST(value AS TYPE)
syntax. For example, CAST('100' AS INT)
will convert the string 100
to the integer value 100. If the cast fails, such as CAST('INT' AS INT)
, the function returns NULL
. In addition, the BINARY
type can only cast to STRING
, then cast from STRING...