CONV converts a number from one base to another and returns the result as a STRING.
Syntax
STRING CONV(<input>, BIGINT <from_base>, BIGINT <to_base>)Parameters
| Parameter | Required | Description |
|---|---|---|
input | Yes | The number to convert. Must be of the STRING type. In Hive-compatible mode (set odps.sql.hive.compatible=true;), implicit conversion from BIGINT and DOUBLE is supported. In non-Hive-compatible mode (set odps.sql.hive.compatible=false;), BIGINT and DOUBLE inputs are not supported and return null. |
from_base | Yes | The base of the input value. Valid values: 2, 8, 10, and 16. No other values are accepted. Implicit type conversion is not supported. |
to_base | Yes | The base of the output value. Valid values: 2, 8, 10, and 16. No other values are accepted. Implicit type conversion is not supported. |
Return value
Returns a value of the STRING type. The following table describes the return value for each condition:
| Condition | Non-Hive-compatible mode | Hive-compatible mode |
|---|---|---|
input, from_base, or to_base is null | null | null |
| 64-bit overflow (input exceeds 64-bit range) | null | null |
input is of the BIGINT or DOUBLE type | null (implicit conversion not supported) | Implicitly converts to STRING, then proceeds with conversion |
input is a negative value | null | 0 |
input is a decimal value | null | Truncates the decimal part and converts the integer portion |
The conversion runs at 64-bit precision. If an overflow occurs, null is returned.
Sample data
The following examples use the mf_math_fun_t table. Create the table and insert sample data with these statements:
CREATE TABLE IF NOT EXISTS mf_math_fun_t(
int_data INT,
bigint_data BIGINT,
double_data DOUBLE,
decimal_data DECIMAL,
float_data FLOAT,
string_data STRING
);
INSERT INTO mf_math_fun_t VALUES
(null, -10, 0.525, 0.525BD, CAST(0.525 AS FLOAT), '10'),
(-20, null, -0.1, -0.1BD, CAST(-0.1 AS FLOAT), '-10'),
(0, -1, null, 20.45BD, CAST(-1 AS FLOAT), '30'),
(-40, 4, 0.89, null, CAST(0.89 AS FLOAT), '-30'),
(5, -50, -1, -1BD, null, '50'),
(-60, 6, 1.5, 1.5BD, CAST(1.5 AS FLOAT), '-50'),
(-1, -70, -7.5, -7.5BD, CAST(-7.5 AS FLOAT),null ),
(-80, 1, -10.2, -10.2BD, CAST(-10.2 AS FLOAT), '-1' ),
(9, -90, 2.58, 2.58BD, CAST(2.58 AS FLOAT), '0'),
(-100, 10, -5.8, -5.8BD, CAST(-5.8 AS FLOAT), '-90');To view the table:
SELECT * FROM mf_math_fun_t;
-- Result:
+------------+-------------+-------------+--------------+------------+-------------+
| int_data | bigint_data | double_data | decimal_data | float_data | string_data |
+------------+-------------+-------------+--------------+------------+-------------+
| NULL | -10 | 0.525 | 0.525 | 0.525 | 10 |
| -20 | NULL | -0.1 | -0.1 | -0.1 | -10 |
| 0 | -1 | NULL | 20.45 | -1.0 | 30 |
| -40 | 4 | 0.89 | NULL | 0.89 | -30 |
| 5 | -50 | -1.0 | -1 | NULL | 50 |
| -60 | 6 | 1.5 | 1.5 | 1.5 | -50 |
| -1 | -70 | -7.5 | -7.5 | -7.5 | NULL |
| -80 | 1 | -10.2 | -10.2 | -10.2 | -1 |
| 9 | -90 | 2.58 | 2.58 | 2.58 | 0 |
| -100 | 10 | -5.8 | -5.8 | -5.8 | -90 |
+------------+-------------+-------------+--------------+------------+-------------+Examples
Static values
-- Returns 12
SELECT CONV('1100', 2, 10);
-- Returns C
SELECT CONV('1100', 2, 16);
-- Returns 171
SELECT CONV('ab', 16, 10);
-- Returns AB
SELECT CONV('ab', 16, 16);
-- Returns NULL (from_base is null)
SELECT CONV('1100', null, 10);Table data
Convert the bigint_data, double_data, decimal_data, and string_data columns to binary:
SELECT CONV(bigint_data,10,2) AS bigint_new, CONV(double_data,10,2) AS double_new, CONV(decimal_data,10,2) AS decimal_new, CONV(string_data,10,2) AS string_new FROM mf_math_fun_t;Result:
+------------+------------+-------------+------------+
| bigint_new | double_new | decimal_new | string_new |
+------------+------------+-------------+------------+
| NULL | 0 | 0 | 1010 |
| NULL | NULL | NULL | NULL |
| NULL | NULL | 10100 | 11110 |
| 100 | 0 | NULL | NULL |
| NULL | NULL | NULL | 110010 |
| 110 | 1 | 1 | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 10 | 10 | 0 |
| 1010 | NULL | NULL | NULL |
+------------+------------+-------------+------------+Related functions
CONV is a mathematical function. For more information about functions related to data computing and conversion, see Mathematical functions.