Apache Drill supports the data type conversion functions that are used to convert a value from one type to another type. For example, we can cast the numeric or string value to a decimal value, and the string that has the hexadecimal encoding of a sequence of bytes can be converted to VARBINARY and so on.
Apache Drill supports the following list of Data Casting functions.
- TO_CHAR(expression, format)
- TO_DATE(expression, format)
- TO_NUMBER(VARCHAR, format)
- TO_TIME (expression [, 'format'])
- CAST (expression AS data_type
- STRING_BINARY(expression)
- BINARY_STRING(expression)
Let us see the Apache Drill Data type conversion function in detail.
1. Apache Drill TO_CHAR Function
Apache Drill TO_CHAR function is used to convert the date, time, number in the character string.
Let us see the syntax of the Apache Drill TO_CHAR function.
TO_CHAR Function Syntax:
The below is the syntax of the Apache Drill TO_CHAR function. We can pass the date, integer, time, double, float data in expression.
TO_CHAR (expression, 'format')
TO_CHAR Examples
In the following example, we will convert the FLOAT/integer/date/time/timestamp data types into a character string in a sequence.
Command:
Output:
2. Apache Drill TO_DATE Function
Apache Drill TO_DATE function is used to convert a character string into a date.
Let us see the syntax of the Apache Drill TO_DATE function.
TO_DATE Function Syntax:
The below is the syntax of the Apache Drill TO_DATE function. In the expression, we can pass the character string in a single quotation.
TO_DATE (expression [, 'format'])
TO_DATE Examples
In the below example, the first command will convert a character to a date format and the second command will extract the year.
Command:
Output:
3. Apache Drill TO_NUMBER Function
Apache Drill TO_NUMBER function is used to convert the character string using a format specification.
Let us see the syntax of the Apache Drill TO_NUMBER function.
TO_NUMBER Function Syntax:
The below is the syntax of the Apache Drill TO_NUMBER function in which a string represents the character and the format represent the value such as "#" represent the Digit placeholder, "0" represent the Digit placeholder if the string has a Digit placeholder, on the place of "0" then that will appear otherwise "0" will appear, "." represents the Decimal point, "," represents the Comma grouping separator and the "E" represents the Exponent.
TO_NUMBER ('string', 'format')
TO_NUMBER Examples
The following are some of the examples of the Drill TO_NUMBER function.
Command:
Output:
4. Apache Drill TO_TIME Function
Apache Drill TO_TIME function is used to convert the character to time data format.
Let us see the syntax of the Apache Drill TO_TIME function.
TO_TIME Function Syntax:
The below is the syntax of the Drill TO_TIME function in which expression is a character string that is enclosed with the single quotation and the format represents the formatting that will be done on an input expression.
TO_TIME (expression [, 'format'])
TO_TIME Examples
The following are some of the examples of the Drill TO_TIME function in which the first command is converting the character to a time format and the second command is converting 81675000 milliseconds to the time.
Command:
Output:
5. Apache Drill CAST Function
Apache Drill CAST function is used to convert an expression from one type to another type.
Let us see the syntax of the Apache Drill CAST function.
CAST Function Syntax:
Drill CAST function has the below syntax in which the expression represents the combination of value, SQL functions operators, etc, and the data_type represents the type in which the CAST function will convert the expression.
CAST ( AS )
CAST Data Type Conversion Examples
Let us see the example to Cast a number to string data type and a string data type to the numeric data type.
Cast a Character String to a Number Data Type
We can Cast a Character String to a Number Data using the CAST function.
In the following example, we will cast a character to the DECIMAL with two decimal places.
Command:
Output:
Cast a Number to a Character Data Type
We can Cast a Number to a Character string using the CAST function.
In the below example we see that the Drill is casting the number to a VARCHAR/CHAR that has 3 bytes. After processing it is showing three characters of the string.
Command:
Output:
6. Apache Drill STRING_BINARY Function
Apache Drill STRING_BINARY function is used to print the bytes. It prints the hexadecimal representation for a byte that is not printable.
Let us see the syntax of the Apache Drill STRING_BINARY function.
STRING_BINARY Function Syntax:
Apache Drill STRING_BINARY Function has the below system in which the expression represents the byte array such as {(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe}.
STRING_BINARY(expression)
STRING_BINARY Example
We will use the STRING_BINARY function with CONVERT_TO function for casting as mentioned below example.
Command:
STRING_BINARY(CONVERT_TO(1, 'INT')) as a,
STRING_BINARY(CONVERT_TO(1, 'INT_BE')) as a_be,
STRING_BINARY(CONVERT_TO(1, 'BIGINT')) as b,
STRING_BINARY(CONVERT_TO(1, 'BIGINT')) as b_be,
STRING_BINARY(CONVERT_TO(1, 'INT_HADOOPV')) as b_be
FROM (VALUES (1));
Output:
7. Apache Drill BINARY_STRING Function
Apache Drill BINARY_STRING function is used to convert hexadecimal encoding of a sequence of bytes into the VARBINARY value.
The following is the syntax of the Apache Drill BINARY_STRING function.
BINARY_STRING Function Syntax:
The below is the syntax of the Drill BINARY_STRING function in which the expression represents the hexadecimal string for example "\xca\xfe\xba\xbe"
BINARY_STRING(expression)
BINARY_STRING Example
In the below example we will convert the hexadecimal string \x00\x00\x00\xC8 into an integer equivalent. After processing the below output will be shown.
Command:
FROM (VALUES (1));